Solved

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

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 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