Solved

How to get all emails from ActiveDirectory using PowerShell without using Get-AdGroupMember?

Posted on 2016-09-20
4
79 Views
Last Modified: 2016-09-20
I'm trying to download all emails from AD groups using PowerShell. The following recursive command works for most groups except any with more than 5000 members (including sub-groups).

Get-AdGroupMember "groupname" -recursive | Get-AdUser -properties Emailaddress | Select EmailAddress | Export-Csv c:\list.csv

Open in new window


The following error occurs with the recursive command returning more than 5000 members:

Get-ADGroupMember : The size limit for this request was exceeded.

The issue I have is my IT department will not increase the max member allowed to be queried (5000) on the AD web server.

A quick web search will yield the following solution but it is not recursive, and I need it to be recursive to get the emails from the sub groups (as we have many sub-groups).

$grp = get-adgroup "groupname" -properties members 
$grp.members | get-aduser | Select Name | Export-csv C:list.csv

Open in new window


Is there any other way to download all emails?
0
Comment
Question by:intoxicated_curveball
  • 2
4 Comments
 
LVL 49

Assisted Solution

by:Akhater
Akhater earned 100 total points
ID: 41807102
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41807145
Try..
Function Get-GrpMember ($Group){
Get-ADGroup $Group -Properties Members | 
 Select-Object -ExpandProperty Members | 
  Get-ADObject -properties Samaccountname | %{
	If ($_.ObjectClass -eq "user"){
	 $_ | Get-aduser -Properties * | ?{$_.EmailAddress} | Select EmailAddress
	}ElseIf($_.ObjectClass -eq "group"){
	 Get-GrpMember $_
	}
  }
}
Get-GrpMember "groupname" | Export-Csv c:\list.csv -nti

Open in new window

1
 

Author Comment

by:intoxicated_curveball
ID: 41807329
Subsun, worked great! Minor issue: it does return duplicates (from sub-groups, not a big deal). Is there anyway to prevent duplicates initially so it doesn't have to be done post-processing?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 400 total points
ID: 41807344
change last line to..
Get-GrpMember "groupname" | Select * -Unique | Export-Csv c:\list.csv -nti

Open in new window

Or
Function Get-GrpMember ($Group){
Get-ADGroup $Group -Properties Members | 
 Select-Object -ExpandProperty Members | 
  Get-ADObject -properties Samaccountname | %{
	If ($_.ObjectClass -eq "user"){
	 $_ | Get-aduser -Properties * | ?{$_.EmailAddress} | Select EmailAddress
	}ElseIf($_.ObjectClass -eq "group"){
	 Get-GrpMember $_
	}
  } | Select * -Unique
}
Get-GrpMember "groupname" | Export-Csv c:\list.csv -nti

Open in new window

1

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question