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!
Anthony K O365Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amit KumarCommented:
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

Anthony K O365Author Commented:
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!
Amit KumarCommented:
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
SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

Anthony K O365Author Commented:
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!
Amit KumarCommented:
PLease check this one.

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

get-adgroup -Filter * -SearchBase "OU=Groups,DC=corp,DC=ourcompany,DC=Com" | ForEach-Object {

	"`r`n$($_.Name)`r`n=============" | Add-Content $saveto
	Get-ADGroupMember $_ | ForEach-Object {
				"," + $_.Name + " (" + $_.SamAccountName + ")" | Add-Content $saveto
	}
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony K O365Author Commented:
Thank you!
Anthony K O365Author Commented:
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?.
Anthony K O365Author Commented:
Disregard last comment. I see the issue. Thanks!
Amit KumarCommented:
Great to know
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.