Managing Distribution Groups in Exchange Labs with PowerShell

Dynamic Distribution Groups serve a purpose, but they are not for everyone.  They tend to be used more for communicating with bigger groups of people in an organization or sometimes when you do not want people to be able to browse group membership…because that is the challenge with them, a user browsing the Exchange Global Address List cannot expand the group membership; membership only gets expanded by Exchange when it needs to work out who is in the group based on a filter calculation.

So that leaves us with regular Distribution Groups where of course membership can be viewed/expanded if permitted, but presents its own unique challenge in that adding uses to a regular Distribution Group can take a long time if there are many users to add.  This is where our friend PowerShell comes in 🙂  It is really straight forward to manage groups memberships with a basic few lines of code and a csv file.

The PowerShell command to add a mailbox/contact/mailuser (e.g. John Doe) to a group (e.g. AllStudents) is:

Add-DistributionGroupMember -Identity AllStudents -Member ‘John Doe’

…and to remove a mailbox/contact/mailuser (e.g. John Doe) from a group (e.g. AllStudents) is:

Remove-DistributionGroupMember -Identity AllStudents -Member ‘John Doe’

As Technet shows, you can actually use a variety of ways to identify the user and group to add or remove them.  So for example

Add-DistributionGroupMember -Identity AllStudents@contoiso.edu -Member john.doe@contoso.edu

…will also work.

"But I have 50,000 users." I hear you say…what can PowerShell do to help me?  Well that is where basic scripting skills come in…and I have just spent 15 minutes knocking up sample (use at your own risk) scripts that takes a simple csv file that I created in Excel, and uses the information in it to add users to groups and remove users from groups.  There are only 2 columns…

I created 2 separate scripts just for cleanliness…you will see that they are identical apart from one key line (and some comments).   I have also provided a csv file for you to use.

Usage is simple:

  1. Start PowerShell CTP
  2. enter:
    $LiveCred = Get-Credential
  3. enter:
    your admin username and password to the popup dialog
  4. to add group members enter, with the right drive paths:
    c:\add_to_dg.ps1 -LiveCredential $LiveCred -RemoteURL https://ps.exchangelabs.com/powershell/ -UsersFile "c:\groups.csv"
  5. to remove group members enter, with the right drive paths:
    c:\remove_from_dg.ps1 -LiveCredential $LiveCred -RemoteURL https://ps.exchangelabs.com/powershell/ -UsersFile "c:\groups.csv"

The script creates and deletes a runspace for you…but if you terminate the script (DON’T CLOSE PowerShell) remember to do ‘Get-RunSpace | Remove-RunSpace’ to kill of any unwanted runspaces.

I did not include any advanced error handling or logging in my script just for the sake of time.  If you want to do this, there are some best practices in the CSV_Parser.ps1 file that you can get from the connect site, or here.

I have not yet covered how to bulk create Distribution Groups with PowerShell, and I wasn’t planning to.  It is pretty straight-forward…and the scripts I provided in this post can be modified quite easily…commands you need are here.

Enjoy!

Jonny

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

%d bloggers like this: