Solved

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

Posted on 2016-09-20
4
94 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
A hard and fast method for reducing Active Directory Administrators members.
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

736 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