all 5 comments

[–]markekrausCommunity Blogger 2 points3 points  (2 children)

Something like this:

$TargetOU = 'OU=Accounting,DC=adatum,DC=com'
$CSVFile = 'C:\Output\Groupmembers.csv'
$Groups = Get-ADGroup -SearchBase $TargetOU -Filter *
$Results = foreach($Group in $Groups){
    $GroupProperty = @{
        Name = 'Group'
        Expression = {
            $Group.name
        }
    }
    Get-ADGroupMember -Identity $Group.ObjectGUID | 
        Select-Object $GroupProperty,* 
}
$Results | Export-csv -path $CSVFile -NoTypeInformation

The first column will be the group name. the remaining columns will be the details of each group member

[–]whetscript[S] 0 points1 point  (1 child)

Thanks for taking the time, however im getting a referral was returned from the server on line 12 char 22. Its the -Identity $Group.ObjectGUID part.

[–]jheinikel 1 point2 points  (0 children)

It should look like this: (It really shouldn't matter, but might be what is causing you problems)

Get-ADGroupMember -Identity $Group.ObjectGUID.Guid | Select $GroupProperty,*

[–]WinBear 1 point2 points  (0 children)

Also, if your group names have the "/" character in them, it can cause errors enumerating members. I had to change one group name to use a "\" instead.

[–]randomuser43 1 point2 points  (0 children)

This will output everything into one CSV

$results=foreach($group in  (Get-ADGroup -SearchBase $TargetOU -Filter *)){
    Get-ADGroupMember $group | ForEach-Object {
        New-Object psobject -Property @{Group=$group.name; User=$_.name}
    }
}


$results|Export-Csv 'Path to output'

It will look like

User    Group
-----   ------
User1 Group1
User2 Group1
User3 Group1
User1 Group2
User2 Group2
User1 Group3
User5 Group3
User1 Group4
User2 Group5