Forum Discussion
Baron164
Jul 14, 2022Brass Contributor
How can I add group membership information to this csv export?
I am currently exporting a list of user accounts which have uidNumbers. So far so good, but now I need to also include in this export, group information. Specifically the gidNumber for each group eve...
- Jul 19, 2022
$fullReport=@() $AllUsers=Get-ADUser -Filter "uidNumber -ge 0" -Properties Name,givenName,sn,uidNumber,userPrincipalName foreach ($singleuser in $AllUsers){ $Report=[PSCustomObject]@{ Name = $singleuser.Name givenName=$singleuser.GivenName sn=$singleuser.sn uidNumber=$singleuser.uidNumber userPrincipalName=$singleuser.userPrincipalName } $AllGroups=Get-ADPrincipalGroupMembership $singleuser.SamAccountName for ($i = 0; $i -lt $AllGroups.name.count; $i++) { $GroupGid=Get-ADGroup -Properties gidNumber -Identity $AllGroups[$i].SamAccountName $Report | Add-Member -NotePropertyName "Group$i" -NotePropertyValue $GroupGid.gidNumber } $fullReport+=$Report }
Let me know 🙂
farismalaeb
Jul 15, 2022Iron Contributor
You will need to use a custom object to group all the content together. here is an example
$fullReport=@()
$AllUsers=Get-ADUser -Filter "uidNumber -ge 0" -Properties Name,givenName,sn,uidNumber,userPrincipalName
foreach ($singleuser in $AllUsers){
$Report=[PSCustomObject]@{
Name = $singleuser.Name
givenName=$singleuser.GivenName
sn=$singleuser.sn
uidNumber=$singleuser.uidNumber
userPrincipalName=$singleuser.userPrincipalName
}
$AllGroups=Get-ADPrincipalGroupMembership $singleuser.SamAccountName
for ($i = 0; $i -lt $AllGroups.name.count; $i++) {
$Report | Add-Member -NotePropertyName "Group$i" -NotePropertyValue $AllGroups[$i].name
}
$fullReport+=$Report
}
The result looks like the following
Name : vdi2
givenName : vdi2
sn : 3
uidNumber : 1
userPrincipalName : email address removed for privacy reasons
Group0 : Domain Users
Group1 : Discovery Management
Group2 : Limited
Group3 : Win 10 Pro - DC
Group4 : High MGMT
Group5 : InTunePOC
- Baron164Jul 18, 2022Brass ContributorSo far so good, the only issue I have is that it's showing the group names instead of the gidNumber attribute. I tried changing the $AllGroups.name to $AllGroups.gidNumber but that does not work.
- farismalaebJul 18, 2022Iron ContributorJust to confirm, So you dont need the group name, you need to have the gidNumber
So the output looks like
Name : vdi2
givenName : vdi2
sn : 3
uidNumber : 1
userPrincipalName : email address removed for privacy reasons
Group0 : 1231231
Group1 : 123134234534
yes?- Baron164Jul 18, 2022Brass ContributorYes, that is correct.