Powershell: list all DL’s and export

The next several posts are going to be quick code drops for me. I’m working on a few process to export everything from one GAL and import it to another one. Partly using ADMT (scripted) and partly using Powershell with the Exchange EMC.

This particular code snippet gets all Distribution Lists in Exchange and dumps them to a file. Easily modifiable for other purposes if you need to:

$results=@()
foreach ($item in $(get-distributiongroup -resultsize unlimited)){$results+=$item.name}
$results|out-file c:\migration\groupout.txt

Let’s break this down a little bit:

$results=@() <== explicitly creates an array for population later

foreach ($item in $(get-distributiongroup -resultsize unlimited)){$results+=$item.name} <== Several things going on in this line. I like combining statements into as little possible code as I can, so this one can be confusing if you don’t know how to look at it

  • foreach (blah in blah){blah} <==typical for each statement that does the {} for every item in the list
  • $item in $(get-distributiongroup -resultsizeunlimited) <== Pulls every DL with no limitations and puts them into the array

$results+=$item.name <== Pulls the name attribute from every DL and adds it into the array defined above

$results|out-file filename.txt <== Outputs the results of the array into a text file

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s