How to make list users and user member groups

Welcome Forums General PowerShell Q&A How to make list users and user member groups

Viewing 15 reply threads
  • Author
    Posts
    • #212856
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      Hi,

      I’ll try to make script where I get users and groups to which they belong, but group search must have argument “-like groupname*”

       

      Example:

      User, group1,group2

       

      My script:

       

      $OU = "OU=test,DC=domain,DC=local"
      $List = Get-ADUser -Filter * -Properties samaccountname, memberof -SearchBase $OU | 
          ForEach-Object {
              $user = $_
              $user.memberof |
                  ForEach-Object {
                      [PSCustomObject]@{
                          SamAccountName = $user.samaccountname
                          Group = Get-ADPrincipalGroupMembership -Identity $_ | select name  | Where-Object {$_.Name -like "groupname*"}
                          }
                      }
                  }
      
      • This topic was modified 2 months, 1 week ago by dr_clayss.
      • This topic was modified 2 months, 1 week ago by dr_clayss.
    • #212868
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      … but group search must have argument “-like groupname*”

      What do you mean with “… must have argument -like groupname”?

      That’s actually all you need:

      $OU = "OU=test,DC=domain,DC=local"
      Get-ADUser -Filter * -Properties memberof -SearchBase $OU | 
          ForEach-Object {
              [PSCustomObject]@{
                  User = $_.sAMAccountName
                  Groups = ($user.memberof) -join ','
              }
      }
    • #212877
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      … but group search must have argument “-like groupname*”

      What do you mean with “… must have argument -like groupname”?

      I don’t want to list all groups, but only what I need

    • #212907
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      I don’t want to list all groups, but only what I need

      Could you elaborate this a little bit more please?

    • #212949
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      Users has member a lot of groups.

      I have groups(examples)

      • dfs_share1_folder1
      • dfs_share1_folder2
      • dfs_share1_folder3
      • dfs_share2_folder1
      • dfs_share2_folder2
      • etc.

      I want to filtered it and find groups for example “name -like “dfs_share1_*” when I don’t need other groups where user is member

    • #212952
      Participant
      Topics: 12
      Replies: 1623
      Points: 2,565
      Helping Hand
      Rank: Community Hero

      Based on Oleg…err @Olaf’s code, try something like this:

      $OU = "OU=test,DC=domain,DC=local"
      Get-ADUser -Filter {MemberOf -like 'dfs_share1_*'} -Properties memberof -SearchBase $OU | #Find only users that are a member of the group
          ForEach-Object {
              [PSCustomObject]@{
                  User = $_.sAMAccountName
                  Groups = ($user.memberof | Where{$_.MemberOf -like 'dfs_share1_*') -join ',' #Show only the groups that you care about
              }
      }
      
    • #212955
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Hmmm … close …

      if I’m not wrong it should be instead of this

      Groups = ($user.memberof | Where{$_.MemberOf -like 'dfs_share1_*') -join ',' 

      more like this:

      Groups = ($user.memberof | Where{$_ -like 'dfs_share1_*'}) -join ',' 

      😛 😉

    • #213192
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      Based on Oleg…err @Olaf’s code, try something like this:

      This line doesn’t work

       Get-ADUser -Filter {MemberOf -like ‘dfs_share1_*’} -Properties memberof -SearchBase $OU 

      Empty result.

      • This reply was modified 2 months ago by dr_clayss.
    • #213270
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Now that you write it in a single line … of course it cannot work. MemberOf is supposed to be an array. Try this:

      $OU = "OU=test,DC=domain,DC=local"
      Get-ADUser -Filter * -Properties memberof -SearchBase $OU | #Find only users that are a member of the group
          ForEach-Object {
              [PSCustomObject]@{
                  User = $_.sAMAccountName
                  Groups = ($_.memberof | Where-Object{$_ -like 'dfs_share1_*'}) -join ','  #Show only the groups that you care about
              }
      }

      BTW: You are allowed to use our suggestions and use it for your own research and play with it to make it run.

      • This reply was modified 2 months ago by Olaf. Reason: corrected code $user.memberof -> $_.memberof
    • #214617
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      It’s doesn’t work.

    • #214620
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      It’s doesn’t work.

      Damn. That happens when you copy code and change it without testing … changed the code above …

      But as I said before: You are allowed to change our suggestions accordingly and debug it when we do something wrong. 😉

    • #214758
      Participant
      Topics: 2
      Replies: 5
      Points: 17
      Rank: Member

      try this

      $groups = "grp1", "grp2","grp3"
      foreach ($group in $groups) {
          Get-ADGroupMember $group | Get-ADUser -Properties samaccountname, userprincipalname |
          Select-Object  samaccountname, userprincipalname,@{n='GroupName';e={$group}}
      }

       

    • #214761
      Participant
      Topics: 2
      Replies: 5
      Points: 17
      Rank: Member

      @rob … how do you insert a block of powershell code ?

    • #214764
      Participant
      Topics: 2
      Replies: 5
      Points: 17
      Rank: Member

      help

      • This reply was modified 2 months ago by robertrobert59. Reason: testing
    • #214842
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      @rob … how do you insert a block of powershell code ?

      First you could (re-)read the instructions you find in the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!

      When you create a new post you use the “Textview, you insert the code you want, then you select the code and click on the code tag “PRE” – that’s all. 😉

    • #214923
      Participant
      Topics: 2
      Replies: 5
      Points: 17
      Rank: Member

      okk thanks

Viewing 15 reply threads
  • You must be logged in to reply to this topic.