Skip to main content


Office 365 – Efficiently Connecting to Office 365 Via PowerShell

If you’re an administrator for Office 365, you’re likely very familiar with the fact that while some tasks can be performed in the portal, many need to be done via PowerShell. If you’ve managed to get this far in your IT life without using PowerShell, Office 365 is going to force you to learn it.
Even those that are savvy with PowerShell at times still do things in what I would consider a less than efficient manner. I’ve watched countless admins paste commands into the PowerShell window in order to connect to Office 365. They open up their OneNote or a Notepad file on the desktop every time they want to connect to Exchange Online and paste in the string of commands.
Below is an easier and more efficient way…

The first thing to note is that when connecting to Office 365 via PowerShell, you’re connecting to each workload individually. So Azure AD, Exchange Online, Skype for Business Online and SharePoint Online all have different commands used to connect to them. You can connect to all of them or you can connect to an individual workload.
What I like to do is add functions to my PowerShell profile for connecting to the workloads. I create a function for each workload and then one to load them all; I do, however, connect to Azure AD with each workload.


First you will need to download and install each of the following and then reboot:

  • PowerShell 3.0 or higher (link)
  • Microsoft Online Services Sign-in Assistant (link)
  • Azure Active Directory Module for Windows PowerShell (link)
  • Skype for Business Online Windows PowerShell Module (link)
  • SharePoint Online Management Shell (link)

Creating The Functions

Launch a PowerShell console as an admin and enter the following:

notepad $profile

Now paste the following into the Notepad window and save:

function Connect-AzureAD
  if(-not(Get-Module -name MSOnline)) {Import-Module MSOnline}
  Write-Host "Connecting to Azure AD..."
  $global:Cred = $host.ui.PromptForCredential("Office 365 Admin Credentials", "Please Enter Your Office 365 Admin Account & Password","","")
  Connect-MsolService -Credential $global:Cred
  $host.ui.RawUI.WindowTitle = "Connected to Office 365 as: " + $cred.username
  Write-Host ""
function Connect-ExchangeOnline
  if(-not($Cred)) {Connect-AzureAD}
  Write-Host "Connecting to Exchange Online..."
  $global:SessionEXO = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $global:Cred -Authentication Basic -AllowRedirection
  Import-PSSession $global:SessionEXO | Out-Null
  Write-Host ""
function Connect-SkypeOnline
  if(-not($Cred)) {Connect-AzureAD}
  Write-Host "Connecting to Skype for Business Online..."
  if(-not(Get-Module -name LyncOnlineConnector)) {Import-Module LyncOnlineConnector}
  $global:SessionSFB = New-CsOnlineSession -Credential $global:Cred
  Import-PSSession $global:SessionSFB | Out-Null
  Write-Host ""
function Connect-SharePointOnline
  if(-not($Cred)) {Connect-AzureAD}
  Write-Host "Connecting to SharePoint Online..."
  if(-not(Get-Module -name Microsoft.Online.Sharepoint.PowerShell)) {Import-Module Microsoft.Online.Sharepoint.PowerShell}
  if ($cred.username -like "*") {
    $Tenant = ($cred.username.Substring($cred.username.IndexOf("@") + 1)).Replace("","")
  else {
    $Tenant = Read-Host -Prompt 'Enter the Tenant Name (i.e. Enter "contoso" for "")'
  Connect-SPOService -url https://$ -Credential $global:Cred
  Write-Host ""
function Connect-O365

Using The Functions

Now when you launch PowerShell, you can run any of the following functions:


The “Connect-O365” function will connect to all workloads whereas the others will connect to an individual workload plus Azure AD.

So there you go, no more copy and paste. It’ll take you about 10 minutes to setup and then you’re probably saving a minute for each of the thousands of times you connect in the future.

What Else?

If you’re looking for more PowerShell tips and tricks related to Office 365, make sure you check out the site that Microsoft recently released:

Did you find this article helpful?
Leave a comment below or follow me on Twitter (@JoePalarchio) for additional posts and information on Office 365.
Looking to do some more reading on Office 365?
Catch up on my past articles here: Joe Palarchio.

Thoughts on “Office 365 – Efficiently Connecting to Office 365 Via PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Joe Palarchio

More from this Author

Follow Us