Anyone who has used remote PowerShell with Office 365 knows that the commands to connect up are a little hard to remember. If you use remote PowerShell with any frequency in Office 365, you’ll probably want to look into creating functions to allow you easily connect to Office 365 without having to cut and paste commands everytime. Here’s how you do that.
Open a PowerShell window and type:
notepad $profile
This will open your PowerShell profile file. It might be blank if you haven’t added anything in the past. You’ll then want to paste the following in it:
function o365connect{
$o365cred=get-credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365cred -Authentication Basic -AllowRedirection
Import-PSSession $session
}
function o365disconnect{
get-PSSession
$id = Read-Host “Enter the Session Number You Would Like to Remove”
Remove-PSSession -ID $id
}
Note that the $Session line in the o365Connect function should be a single line that begins with $Sesssion and ends with AllowRedirection (so the function should have 3 lines).
After you’ve entered the information, save the file, close PowerShell and reopen PowerShell. Now just type o365connect and hit enter. It will prompt you for your credentials and connect you to Office 365.
Typing o365Disconnect, will display a list of active sessions and ask you which one you want to disconnect. It’s important to disconnect sessions, as you are limited to a set number of active sessions per organization.
Related blog posts
Get our updates straight to your inbox!
Sign up for our email updates to make sure you don't miss any of our new content.