Useful PowerShell Scripts for Office 365 Migration

I recently took my company of about 140 off of our current POP/SMTP email system (yes, companies still use POP for email) and onto Microsoft’s Office 365 Hosted Exchange. They give you PowerShell access to your Exchange 2013 server so there is a lot you can do to help reduce the time it takes to get your environment ready. Here are some quick and easy PowerShell scripts I wrote to assist with my transition.

I added all of my users by importing a CSV file through the GUI, so all of these scripts I created after my users were already added.

The first one is to connect to your Exchange server. Run this in your PowerShell ISE once, and you’ll have access to all of the new modules for managing your Exchange server.

$exchange = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell -Credential admin@contoso.com -Authentication basic -AllowRedirection
Import-PSSession $exchange
Get-Module
view raw gistfile1.ps1 hosted with ❤ by GitHub

 

The next one I wrote was because I had two sets of O365 licenses (E3 licenses from our Microsoft Partnership, and E1 licenses that I purchased for the remaining users). I only wanted my users to have access to Exchange and Lync, so this script removed the other licensed features. The first part of the script removes the additional features, and the second part assigns the new license back to my users.

e1imports.csv

$limitedE1 = New-MsolLicenseOptions -AccountSkuId Contoso:STANDARDPACK -DisabledPlans YAMMER_ENTERPRISE,SHAREPOINTSTANDARD ;
$limitedE3 = New-MsolLicenseOptions -AccountSkuId Contoso:ENTERPRISEPACK -DisabledPlans YAMMER_ENTERPRISE,OFFICESUBSCRIPTION,SHAREPOINTENTERPRISE,SHAREPOINTWAC ;
Import-Csv c:\powershell\e1imports.csv | ForEach {Set-MsolUserLicense -UserPrincipalName $_.user -LicenseOptions $limitedE1} ;
Import-Csv c:\powershell\e3imports.csv | ForEach {Set-MsolUserLicense -UserPrincipalName $_.user -LicenseOptions $limitedE3} ;
view raw gistfile1.ps1 hosted with ❤ by GitHub

 

This next one I used for creating my external contacts. If you want to have email addresses in your distribution lists that are not members of your domain, you have to add them in as contacts. I then wanted to hide these contacts from showing up in the global address list, so the bottom script does that.

contact.csv

Import-Csv c:\powershell\contact.csv | ForEach {New-MailContact -Name $_.displayname -ExternalEmailAddress $_.emailaddres)}
Import-Csv c:\powershell\contact.csv | ForEach {Set-MailContact -Identity $_.displayname -HiddenFromAddressListsEnabled $true}
view raw gistfile1.ps1 hosted with ❤ by GitHub

 

The next one I used was for adding members into my distribution lists. I used the GUI to make all of my distribution lists, but I then used this script to populate the members. The example below is adding members to the development@contoso.com distribution list.

distribution.csv

Import-Csv c:\powershell\distribution.csv | ForEach {Add-DistributionGroupMember -Identity "development" -Member $_.developers}
view raw gistfile1.ps1 hosted with ❤ by GitHub

 

This next one I wrote because I wanted to have my conference rooms appear in a Room list, so I could separate them by which office they were located in. I used the GUI to create my conference rooms, but here is how I created the room lists, and added the conference rooms for each office.

New-DistributionGroup -Name "123 Main Street" -RoomList
New-DistributionGroup -Name "45 East Street" -RoomList
Add-DistributionGroupMember -Identity "123 Main Street" -Member "2nd floor left side conference room"
Add-DistributionGroupMember -Identity "123 Main Street" -Member "1st Floor Conference Room"
Add-DistributionGroupMember -Identity "123 Main Street" -Member "2nd Floor main conference room"
Add-DistributionGroupMember -Identity "123 Main Street" -Member "2nd Floor right side conference room"
Add-DistributionGroupMember -Identity "123 Main Street" -Member "3rd Floor main conference room"
Add-DistributionGroupMember -Identity "45 East Street" -Member "Front conference room"
Add-DistributionGroupMember -Identity "45 East Street" -Member "Back conference room"
view raw gistfile1.ps1 hosted with ❤ by GitHub

 

This last one I used was to send all of my users their temporary passwords and a how-to guide for changing their temporary password and adding their new account to Outlook. I copied their email addresses and passwords straight from the GUI after I created the user accounts and pasted them into a CSV file.

emailpassword.csv

Import-Csv c:\powershell\emailpassword.csv | ForEach {Send-MailMessage -to $_.email -subject "Office 365: Tempoary
Email Password" -from "admin@contoso.com" -BodyAsHtml "<br> Username: $($_.email) <br> Passsword:
$($_.password) <br><br> Please log into https://outlook.office365.com to continue." -SmtpServer smtpserver.contoso.com -Attachments
c:\powershell\connectionguide.docx}
view raw gistfile1.ps1 hosted with ❤ by GitHub

 

 

And that’s all! If you have any questions on how these work feel free to leave a comment.

 

Cheers!

 

 

 

3 thoughts on “Useful PowerShell Scripts for Office 365 Migration