3

I'm trying to script something where I can get the email address of all users in an active directory security group.

What I have so far:

$Groups = Get-ADGroup -filter {Name -like "VIPEmail" } | Select-Object Name
ForEach ($Group in $Groups) {
  Get-ADGroupMember -identity $($group.name) -recursive | Select-Object samaccountname
}

Obviously this will only return the samaccountname, which it does. I replace samaccountname with EmailAddress, and it does nothing.

5 Answers 5

3

To keep with your original formatting so you can see where you went wrong:

$Groups = Get-ADGroup -filter {Name -like "09-Admins" } | Select-Object Name
ForEach ($Group in $Groups) {
   Get-ADGroupMember -identity $($group.name) -recursive | Get-ADUser -Properties mail | Select-Object mail
}

The problem is that you are trying to read a property that the Get-ADGroupMember return type doesn't have (Microsoft.ActiveDirectory.Management.ADPrincipal). You need to pipe that return into Get-ADuser AND specify that you want it to pull the emailaddress property. Get-ADUser will not pull most properties of a user by default, so you need to specify any additional properties you would like to retrieve (or just select all of them with "-Properties *", but that's kind of sloppy).

Mjolinor beat me to the answer, but I figured it was worth elaborating a little on his response.

3
  • I like this answer better :) One thing though if you can assist. What I'm trying to do is migrate all of the users in the group VIPEmail to another exchange database. How do I use the results "mail" with my move command?
    – DobotJr
    Jan 15, 2014 at 23:27
  • I haven't done a huge amount of work in the Exchange Shell, but I found an article that may help. link It looks like New-MoveRequest just needs an -Identity parameter to determine what it targets, which gives you quite a bit of leeway on how you target the request. Jan 16, 2014 at 1:50
  • Yeah, I know how to move the mailbox using New-MoveRequest -Identity $emailaddress -TargetDatabase "DB01", my issues is assigning the variable $emailaddress with the email addresses that i'm pulling from the group VIPEmail. Does that make sense?
    – DobotJr
    Jan 16, 2014 at 2:25
3

Something like this:'

Get-ADGroup -filter {name -like 'VIPEmail'} | 
Get-ADGroupMember -Recursive |
Get-ADUser -Properties Mail |
select -ExpandProperty Mail
1
  • thanks that works. One issue, i'm getting an error "select : Cannot process argument because the value of argument "obj" is null", anyways to keep going if the value mail is null?
    – DobotJr
    Jan 15, 2014 at 23:06
0

I'd add unique. So when a user is in more groups only one email is shown:

Get-ADGroup -filter {Name -like "RO*" } | 
Get-ADGroupMember -Recursive | 
select -Unique| 
get-adUser -Properties mail |
select -ExpandProperty Mail
0
Get-ADGroupMember GroupName | get-aduser -properties mail | select mail | Sort-Object mail

If you have groups nested you can change the above to:

Get-ADGroupMember -Recursive GroupName | get-aduser -properties mail | select mail | Sort-Object mail
0

I went with:

Get-ADGroupMember -Identity your_ad_group_name_here -Recursive | Get-ADUser -Properties *  | Select SamAccountName,mail,EmailAddress,Manager

...just make sure you've installed the necessary powershell module so you have access to Get-ADGroupMember. You can download the WindowsTH-RSAT_WS_* specific to your version of Windows 10 here - https://www.microsoft.com/en-us/download/details.aspx?id=45520.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.