Solved

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

Posted on 2016-09-20
4
44 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

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
In this video we show how to create a Shared Mailbox 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 Recipients >> Sha…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now