I have a list of users and I need to know what their Active Directory Group memberships are. I need them to group together in a csv something like this with the username, groupname, and grouptype (Security of Distribution) but I'm not having much luck. Below is the Powershell script I tried to make up and use.
$Users = Get-Content -Path 'C:\Scripts\Lists\UserDistro.txt'
Foreach ($user in $users) {
$GroupName = Get-ADPrincipalGroupMembership $user | Select-Object -Property Name
$GroupType = Get-ADPrincipalGroupMembership $user | Select-Object -Property GroupCategory
$Results = @{'Username'=$User;'Group'=$GroupType}
$obj = New-Object -TypeName PSObject -Property $Results
Write-Output $Obj | Format-table -AutoSize
The output I'm getting looks like this:
Username Group GroupType
-------- ----- ---------
psmith {@{Name=Domain Users}, @{Name=Group1}, @{Name=Group2}, @{Name=Group3:}...} {@{GroupCategory=Security}, @{GroupCategory=Security}, @{GroupCategory=Security}, @{...
The issues I'm having are
- The list is truncated, there should be more groups than what's showing here
- I don't need all this peripheral information @{Name= Just the group name
- How can I sort this so the group name and group type lineup?