Link to home
Start Free TrialLog in
Avatar of intoxicated_curveball
intoxicated_curveball

asked on

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

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?
SOLUTION
Avatar of Akhater
Akhater
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of intoxicated_curveball
intoxicated_curveball

ASKER

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial