Scripting your remote PowerShell connection to Exchange Online

When migrating a customer to Exchange Online, one of the most time consuming parts of the project is training the administrators how to use a new set of interfaces for managing their new Exchange environment. Even administrators who are well versed in PowerShell tend to be unfamiliar with the process of connecting to the remote PowerShell instance of their new Exchange Online tenant. Today I am going to share with you a simple script that will make that learning curve a little easier.

For the purpose of this script, I am going to stick to one my of most important guiding principles when deploying an Exchange environment; K.I.S.S. I’m going to leave off all the bells and whistles from this script and just stick to the part you need. Copy the below text into notepad, and save as a .ps1 file on your management console. I recommend creating a folder for storing scripts, maybe c:\scripts.

$cred = Get-Credential
Connect-MsolService -Credential $cred
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection 
Import-PSSession $session

There are two additional things you need to remember before running this script

  1. You need to run Set-ExecutionPolicy RemoteSigned to allow scripts to execute on your machine.
  2. To run a script in a PowerShell session you need to use a “.\” in front of your script name. I saved the script at “connect-exo.ps1” so to run the script I type “.\connect-exo.ps1”

…and there you go. That is really all you need to automate your remote PowerShell connection to Exchange Online.