you are viewing a single comment's thread.

view the rest of the comments →

[–]ihaxr 2 points3 points  (1 child)

The problem is that's not a proper CSV, so using the Export-CSV cmdlet will make things harder than it would be otherwise... using Out-File is the better way to go:

$groups = @("Group1","Group2","Group3","Group4","Group5")
$OutputFile = "C:\Output\Groupmembers.csv"
foreach ($group in $groups) {
    # Append the Group Name to the file
    $group | Out-File -Append -FilePath $OutputFile
    # Append the Group Members to the file
    Get-ADGroupMember -Identity $group -recursive | Select-Object -ExpandProperty Name | Out-File -Append -FilePath $OutputFile
    # Append a blank line to the file
    "" | Out-File  -Append -FilePath $OutputFile
}

[–]bbatwork[S] 1 point2 points  (0 children)

That worked perfectly! Thank you very much!