MrDevo
MrDevo

Reputation: 35

AD Export of Groups / Members of Each group and Email Addresses

I would like to ask if anyone could help me with a script to extract all the AD groups with their members and their email addresses. I'm running the script bellow which I found it in one of the posts which extracts all the AD Groups and their members but I don't know how to include their email addresses also. Thank you very much for your help.

$Groups = Get-ADGroup -Filter * -SearchBase 'OU,OU,OU,OU,OU,DC,DC,DC' #creates a variable with the name Groups and stores all the groups into it

$Results = foreach( $Group in $Groups ){ #looks for members in each group and stores them in Results

    Get-ADGroupMember -Identity $Group | foreach {

        [pscustomobject]@{

            GroupName = $Group.Name

            Name = $_.Name

            }

        }

    }

$Results| sort -Property GroupName | Export-Csv -Path c:\temp\groups.csv -NoTypeInformation #stores results in a csv

Upvotes: 0

Views: 2791

Answers (1)

LeeM
LeeM

Reputation: 1248

You'll need to capture the user's email address in your foreach loop, and you'll need to do that by looking up the user properties - a listing of group members only has the member DN and name.

Get-ADGroupMember -Identity $Group | foreach {
    $u = get-aduser $_ -properties mail  ##plus any other user properties you need
    [pscustomobject]@{
        GroupName = $Group.Name
        Name = $u.Name
        Email = $u.mail
    }
}

Upvotes: 2

Related Questions