0
votes

I'm trying to get an CSV Output from ADGroupMembers in ADGroups like this:

groupname,member1,member2,member3...

I use the following script:

Get-ADGroup -Filter * -SearchBase "OU=myou,OU=myou,DC=domain,DC=local" | group {$_.name} | % {
$group = $_.name
$member = (Get-ADGroupMember -Identity $group |
  select @{n='Group';e={$group}}, @{n='Member';e={$_.Name}}) -join ','
  '{0},{1}' -f $group,$member
} | Out-File 'out-file-try.csv'

Problem is: my Output shows no members:
Group1,,,,,,,,,,,,,,
Group2,,
Group3,,
Group4,,,,,,,,
Group5,,,,,,,,,,

Can anyone help?

Thanks a lot!

1

1 Answers

0
votes

Your use of select-object and -join are actually not doing what you think they are doing. Your select object statement is creating an array of objects like so:

Group    Member
---------------
G1       M1
G1       M2

And so on. These objects are based on the pscustomobject type. That type evaluates to [string]::Empty in a string context (what happens when you use the -join operator)

Change the select object in the get-adgroupmember pipeline to select-object -expandproperty name and leave everything else the way it is, and you should get the result you desire.