Link to home
Start Free TrialLog in
Avatar of Anthony K O365
Anthony K O365Flag for United States of America

asked on

Exchange 2013 Powershell-- Export members from All Distribution Groups in OU

Need PS script to export All Members from All Distribution Groups in an OU...to csv file.

thanks!
Avatar of Amit Kumar
Amit Kumar
Flag of India image

Please use this one.

$saveto = "D:\Data\Members.txt"

Get-DistributionGroup -organizationalunit "domain/ou" -ResultSize Unlimited | sort name | ForEach-Object {

	"`r`n$($_.Name)`r`n=============" | Add-Content $saveto
	Get-DistributionGroupMember $_ | sort Name | ForEach-Object {
		If($_.RecipientType -eq "UserMailbox")
			{
				"," + $_.Name + " (" + $_.PrimarySMTPAddress + ")" | Add-Content $saveto
			}
	}
}

Open in new window

Avatar of Anthony K O365

ASKER

Question: What is the content of the text file in line one? Or is it simply creating a variable and being used later in line 8 to create the content?

Thank you!
It is actually blank, and data will be written to it. so you can change path as per your choice.

Rest code will fetch the data from DLs and will write to members.txt
I'm sorry. After speaking to the client, these are actually security groups. not DL. Can the script be modified for Security Groups?

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Amit Kumar
Amit Kumar
Flag of India 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
Thank you!
when I run the script i get:

cmdlet ForEach-Object at command pipeline position 2
Supply values for the following parameters:
Process[0]:

Any thoughts?.
Disregard last comment. I see the issue. Thanks!
Great to know