Issue with script AD group membership export


I’ve found a script somewhere over here but I have a small issue with it. The script basically gets all the group members of a list of groups from a csv. It outputs the groups and their members in different columns.

The issue is that when a group contains only one member, it isn’t displayed in the output. Can someone help me figure out what’s wrong ?

#Get the list of groups $Groups = import-csv c:tempgroups.csv # Initialize output object $OutputObject = [System.Collections.Generic.List[pscustomobject]]@{} #Loop through each group to find the members of each group ForEach ($Group in $Groups){ # Get group members $GroupMembers = Get-ADGroupMember -Identity $Group.Name | Select Name # Add rows if there are not enough if($OutputObject.Count -lt $GroupMembers.Count){ $($OutputObject.Count + 1 )..$($GroupMembers.Count) | ForEach-Object { $OutputObject.Add([pscustomobject]@{}) } } # Add the column to each row foreach($Row in $OutputObject){ $Row | Add-Member -Name $Group.Name -MemberType NoteProperty -Value '' } # Add the members to the corrcet column for($Index = 0; $Index -lt $GroupMembers.Count; $Index++){ $OutputObject[$Index].$($Group.Name) = $GroupMembers[$index].Name } } $OutputObject | export-csv C:temptest.csv -NoTypeInformation 

submitted by /u/mikounet
[link] [comments]

Leave a Reply