During the latest snowstorm here in Raleigh we had the need for a new distribution group so that we could communicate between a select group of people who were working from home.
I used PowerShell to create the Distribution Group with the New-DistributionGroup cmdlet
new-DistributionGroup -Name 'Remote Workers' -OrganizationalUnit 'mybiz.local/Groups/Distribution' -SamAccountName 'Remote Workers' -Alias 'RemoteWorkers' set-DistributionGroup -Name 'Remote Workers' -RequireSenderAuthenticationEnabled $falseSidenote: I set RequireSenderAuthenticationEnabled to $false because I wanted this group to be accessible to Internet emails. If I wanted it to be internal only, I would not bother with running this command. I learned quickly with Exchange 2010 that when a new distribution group is created it makes this value $true which prevents emails being sent to the group unless the user is authenticated (a member of your domain).
Now that the group was created, I needed to add approximately 30 users. Fortunately someone had created a spreadsheet detailing these particular users, with columns including:
Last Name, First Name, Mobile Number, Work Extension, Department, Title, Email Address
While I could use some Excel functions to make usernames out of Last Name + First Name, the easiest option here was to use all of the email addresses with a PowerShell command.
I copied the email addresses into Notepad++. To turn this into a PS cmdlet that we can run in the Exchange Management Shell we need to insert the Add-DistributionGroupMember cmdlet before all of the email addresses. I could manually paste this on each line, but that would be annoying. And manual. After adding this to each line I also have to put a closing quotation mark at the end of each line to close the email address value. So if this was for 100 or 200 people, or even 1,000 people if your environment is that large, it would take a long time and a lot of keystrokes.
I would rather spend some time now figuring out how to automate this so that when I need to perform this in the future I can do it with ease. This is where the awesomeness begins!
Now that we’ve pasted our email address list into Notepad++ with each email address on its own line, follow these instructions to turn it all into lines of PowerShell code:
- Press CTRL + H to bring up the Replace window
- check off “Regular expression” at the bottom left
- put a caret ^ in the “Find what” field (this is the regular expression for “the beginning of each line”)
- in the “Replace with” field enter the following:
Add-DistributionGroupMember -identity 'Remote Workers' -member "
- Click “Replace All”
Your Notepad++ window should now like this:
Note that there is a quotation mark before each email address
We aren’t done yet, as we have to close each line with a quotation after the email address.
- In Notepad ++, put a dollar sign $ in the “Find what” field (this is the regular expression for “at the end of each line”)
- in the “Replace with” field enter a quotation mark “
- Click “Replace All”
Now you have a full line of PowerShell code that should look like this:
Add-DistributionGroupMember -identity 'Remote Workers' -member "email@example.com"
Copy the entire Notepad++ window and paste this into your Exchange Management Shell to add all these users to the distribution group:
Don’t forget to press Enter for the last line. Since there is nothing following it, the shell will not process the command automatically as it did with all the previous lines.
Check your distribution group to see that it has its new members and be on your way to the next IT solution!