Powershell : Group members export

Hello experts,

I have the following script and I need to output the members to a csv file with the format, GroupName, username.
For instance:
Group1, testuser1
Group2,testuser2
Group2,testuser1

$G = Get-AdGroup -filter "name -like 'test*'" -properties samaccountname

foreach ($group in $G) {
get-adgroupmember -identity $G.samaccountname | export-csv ./test.csv
}

Looking forward to your assistance.
Parity123Asked:
Who is Participating?
 
oBdACommented:
Like that, for example:
Get-AdGroup -Filter "name -like 'test*'" | ForEach-Object {
	$GroupSam = $_.SamAccountName
	Get-ADGroupMember -Identity $GroupSam | Select-Object -Property @{n='GroupSam'; e={$GroupSam}}, @{n='UserSam'; e={$_.SamAccountName}}
} | Sort-Object GroupSam, UserSam | Export-Csv -NoTypeInformation -Path .\test.csv

Open in new window

0
 
Parity123Author Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.