JRawlings
JRawlings

Reputation: 31

Get Members of AD Group and output in specific manner

There are plenty of guides on how to get the members of specific groups from a text file and output them to a CSV, and I am now well versed in those, however, they don't quite meet my criteria and my knowledge doesn't quite let me get past this boundary.

I have the following

$Filename = "C:\Users\Username\Desktop\AD Groups.txt"
$GroupName = Get-Content $Filename

Get-ADGroupMember -identity $GroupName | select name, $GroupName | Export- 
csv -path C:\Temp\Groupmembers.csv -NoTypeInformation

What I want to do though, is have something that gets the users in the groups specified in the text file and outputs them to a CSV file that looks like this

GroupName    MemberName
Group 1      All the members of that group ...
Group 2      All the members of that group ...

All the current versions I have found do this

Group Name    MemberName
Group 1       Name1
Group 1       Name 2
Group 2       Name 1
Group 2       Name 2
Group 3       Name 1

etc.

Upvotes: 0

Views: 741

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25031

You can do the following, which will create a semi-colon delimited list of members on one line and under one property (MemberName).

$Filename = "C:\Users\Username\Desktop\AD Groups.txt"
$GroupNames = Get-Content $Filename

$Output = foreach ($GroupName in $GroupNames) {
    $members = Get-ADGroupMember -Identity $GroupName
    [pscustomobject]"" | Select @{n="GroupName";e={$groupName}},@{n="MemberName"; e={$members.name -join ";"}}
} 

$Output | Export-Csv -path C:\Temp\Groupmembers.csv -NoTypeInformation

Upvotes: 2

Related Questions