Want to export AD Group Members with email address

All,

Can anyone help in exporting the AD group membership with email address.

Thanks
Vel

This forum is for scripting questions. We do not write scripts on request and we expect you to have attempted to write some code to solve your problem.

If you have a question, please let us know what you’ve already tried and share your code.

You may wish to start by reading the help for Get-ADGroupMember and Get-ADUser

1 Like

Thank you very much , but can you tell the Powershell command to export all the ad groups and their members with the email addresses.

Vel

Did you read the help files in the links above?

You can use the Get-ADGroupMember command to retrieve the group membership. You can then use Get-ADUser for each group member to retrieve their e-mail address.

Hi,
When i an trying to run Get-aduser, i am getting the below error.
Get-ad user: cannot find an object with identity
I am trying to print Group names, group members and their respective email addresses.
Kindly help.

Thanks
Vel

Please share your code.
When posting the code, please use the </> button to format it.

**[quote=“Shanmugavel, post:1, topic:18965, full:true”]
All,

Can anyone help in exporting the AD group membership with email address.

Thanks
Vel

Hi Matt,

Please find the below script i am using, where i am getting the above error

[/quote]

**```
$allgroups = Get-ADGroup -Filter *

$result = foreach ( $group in $allgroups ) {

$hash = @{GroupName=$group.SamAccountName;Member=''}
$groupid = $group.distinguishedname

if ( $members = Get-ADGroupMember $groupid ) {
       
        foreach ( $member in $members ) {
        $mem = get-aduser "$member" -properties UserPrincipalName
            $hash.Member = $mem.UserPrincipalname

            New-Object psObject -Property $hash
        }
}
else {
    $displayname = "No Members"
    $hash.Member = $displayname
    New-Object psObject -Property $hash
}

}

$result | Export-Csv -Path C:\temp\AzureADGroups.csv -NoTypeInformation

Do you get any output at all?

That script looks like it should work. But bear in mind that you’re processing all groups because you’re using Get-ADGroup -Filter *.

If you have groups that have computers in them, then when you run Get-ADUser against the computer object, you will get an error.

Consider using a try/catch block to handle the errors.

You also state that you want the user’s e-mail address, but you haven’t specified the emailAddress property. The user principal name might be the same as the e-mail address, but that’s not always the case.

1 Like

Hi Matt,
I was getting the below error.

Getting the below 2 errors
Get-ADuser: Unable to contact the server. This maybe because this server does not exist it is currently down or it does not have the active directory Web Services running.
Get-ad user: cannot find an object with identity at line: 7 char:8

Thanks
Vel

Hi Matt,
Can you please help me on how to avoid this error.
I am not familiar in using try/ catch, also please let me know if I can use AD-object instead of AD-group.

Thanks
Vel

You could use Get-ADObject with a filter to return only groups but I’m not sure what problem that solves. What’s your thinking?

For the first error you should make sure your PC is joined to the same domain as the domain controller and that you’re properly authenticated. If you’re not connected to the same domain, you should provide the server and and credentials to the Get-ADUser cmdlet

$credential = Get-Credential
$server = mydomaincontroller.contoso.com

Get-ADUser -Identity $member -Server $server -Credential $credential

Have you read the help for try/catch? The examples show how to use it.
Ideally, you would put some code in the catch block to handle the error, but to simply supress the messages, which is what I believe you want to do(?), you can leave it empty.

try {    
    $mem = get-aduser "$member" -properties UserPrincipalName
    $hash.Member = $mem.UserPrincipalname
    New-Object psObject -Property $hash
}
catch {
}

Hi Matt,
How to identify the server name or the DC name the groups belong.

Thanks
Vel

If you’re working with a single domain, the groups will be replicated to all domain controllers in that domain, so just pick an available DC.

You can use Get-ADDomainController to discover an available DC.

assuing you wanted to retrive all the memberships with email address
Get-ADGroupMember -Identity “GroupName” | Get-ADUser -Properties mail,department,country| Select-Object Name,mail, country,department

Rabbani,
Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

( You can go back, edit your existing post and correct the formatting! :wink: )