How to make list users and user member groups

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*"}
                    }
                }
            }

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 ','
        }
}

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

Could you elaborate this a little bit more please?

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

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
        }
}

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 ',' 
:-P ;-)

[quote quote=212952]Based on Oleg…err @Olaf’s code, try something like this:

[/quote]

This line doesn’t work

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

Empty result.

[quote quote=213192][/quote]

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.

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. :wink:

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}}
}
 

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

help

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. :wink:

okk thanks