Setup a Single Office 365 PowerShell Session

Another administrative function I often get asked about is connecting to and administering Office 365 using PowerShell.

The act of connecting to Office 365 using PowerShell is actually quite a straightforward thing to do until you want to interact with ALL Office 365 services in a single session repeatedly.

So to achieve this I have 3 functions contained in my PowerShell Profile

  • Conex
  • get-credentials
  • ConnectToOffice365

The purpose of these are to easily run a single command and connect to all of the O365 Admin Centers.

The main function Conex calls the get-credentials function and, as long as credentials are entered, it then runs the ConnectToOffice365 function which makes the connections to the service admin centers.

function Conex()
  Write-output "Get Credentials to use for sessions"
  $credential = get-credentials
  if( $credential )
    Write-output "Credentials Retrieved, connecting to Office 365"
    ConnectToOffice365 -cred $credential

The next function is a simple call to get-credential to collect the Login ID and Password you would like to use for the tenant connection.

function get-credentials()
  $local:cred = get-credential
  if( $cred )
    write-host "Credentials Loaded" -foreground green
    Return $cred
    write-host "Credentials NOT Loaded" -foreground red
    Return $null

The main function ConnectToOffice365 contains the actual connection calls and makes a connection to each of the following

  • MS Online
  • Exchange Online
  • Skype for Business Online
  • Sharepoint Online
  • Security and Compliance Center

Note: For the Security and Compliance Center CC is prefixed in front of the Noun component of the CMD-LET to distinguish them from the Exchange Online CMD-LET’s such as Get-RoleGroup which, for the Security and Compliance Center, would become Get-CCRoleGroup.

This function also changes the Windows PowerShell session title to append the logged on user. So if you administer more than one tenant, you know which one you are logged onto!

function ConnectToOffice365()
  Write-output "Remove existing PowerShell Sessions"
  get-PSSession | remove-PSSession

  Write-output "Connect to Office 365"
  Import-Module MSOnline
  Connect-MsolService -Credential $credentials
  Write-output "Connect to Exchange Online"
  $ExOSession = New-PSSession -Name "Exchange Online" -ConfigurationName Microsoft.Exchange -ConnectionUri "" -Credential $credentials -AllowRedirection -Authentication "Basic"
  Import-PSSession $ExOSession -AllowClobber

  Write-output "Connect to Skype for Business Online"
  Import-Module SkypeOnlineConnector
  $SfBOSession = New-CsOnlineSession -Credential $credentials
  Import-PSSession $SfBOSession -AllowClobber

  write-output "Connect to Sharepoint Online"
  $TenantID = ( (Get-MsolAccountSku )[0].AccountSkuID ).split( ":" )[0]
  Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
  Connect-SPOService -Url "https://$( $TenantID )" -credential $credentials
  write-output "Connect to Security & Compliance Center Online"
  $SecComSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "" -Credential $credentials -Authentication Basic -AllowRedirection
  Import-PSSession $SecComSession -Prefix CC

  if( $host.UI.RawUI.WindowTitle -notlike "*$( $credentials.username.ToUpper() )*" ){ $host.UI.RawUI.WindowTitle = "$( $host.UI.RawUI.WindowTitle ) - $( $credentials.username.ToUpper() )" }

The download for the PS1 file is linked HERE

PowerShell Admin "Office 365" Connect Remote Connection Remoting Administration Skype business exchange online Yammer Teams unifiedGroup

There are prerequisites to connecting to the Admin centers in this way, such as the following

You need to install the modules that are required for Office 365, SharePoint Online, and Skype for Business Online:

  • Microsoft Online Service Sign-in Assistant for IT Professionals RTW
  • Windows Azure Active Directory Module for Windows PowerShell (64-bit version)
  • SharePoint Online Management Shell
  • Skype for Business Online, Windows PowerShell Module

If you’re operating an older OS such as Windows 7 or 8 then there may be additional prerequisites you need to put onto your device prior to completing connections.

You can check out the Technet Blog post here a more detailed run through of each of the connections.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.