hey people , im trying to get the members of three groups , select SamAccountName,UserPrincipalName and the name of the group it belongs to , i’ve reached this far

$groups = "grp1", "grp2","grp3"
#Write-Output $groups
foreach ($group in $groups) {
    
    Get-ADGroupMember $group | Get-ADUser -Properties samaccountname, userprincipalname |
    Select-Object  samaccountname, userprincipalname
}

i tried adding

,$group

but didn’t work … i tried adding a hash

@{n="group";e="$group"}

also not the results i want …

how can i achieve that? i would like the name of the group to appear next to each user

@thomaslee

6 Spice ups

Can you elaborate?

I assume you mean the group does not show up?
You can use a calculated property to do this.
(see my sample below)

foreach ($group in $groups) {
    
    Get-ADGroupMember $group | Get-ADUser -Properties samaccountname, userprincipalname |
    Select-Object  @{n='GroupName';e={$group}},samaccountname, userprincipalname
}
1 Spice up

OMG i was doing it right … i just missed the brackets around the expression … damn …

thank you anyway …

1 Spice up

2020-04-02_14_40_49-doc_brown_damn_damn_meme_-_Google_Search.png

Glad you got it resolved!

Nealy beat me to it…