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?
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

0
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!
0
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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!
0
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

0

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!
0
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?.
0
Anthony K O365Author Commented:
Disregard last comment. I see the issue. Thanks!
0
Amit KumarCommented:
Great to know
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.