How To: Connect PowerShell to Exchange Online/Skype for Business Online

To start out the new year I thought I would return to something a little more basic but I always need to refer back to when building a new machine for myself or if I lose my PowerShell Profile (read more about those here). Connecting to Exchange Online and/or Skype for Business Online PowerShell.

I rarely use a saved script or enter this code manually as I have it saved in my PowerShell Profile, which runs every time I start PowerShell, but on the rare occasion I am setting up a new machine or working away from my normal PC I need to look this up, so where better to keep it than somewhere where I can share it!

Connecting to Exchange Online

Firstly connecting to Exchange Online, uses the code below:

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

First the script asks you to enter your Office 365 Global Admin credentials, and then it saves these into a variable with the rest of the required session information, and will then import this variable to create a new session and connect to the Microsoft Online service. Simple, and all you have had to do is enter a username and password.

Connecting to Skype for Business Online

Connecting to Skype for Business Online follows exactly the same pattern and the code I use for this is below:

$LiveCred = Get-Credential
$session=new-csonlinesession -Credential $livecred
import-pssession $session
Connect-MsolService -Credential $livecred

Including in your PowerShell Profile

If you, like me, connect to Skype for Business Online or Exchange Online almost every time you open a PowerShell session, then it would make sense that the code above runs automatically in your Profile. Below is my code from my profile that asks if you would like to connect to either Exchange Online/Skype for Business Online and runs the appropriate login script.

$runningExchangeOnline = Read-Host "Are you connecting to Office 365? (Y/N)"
if($runningExchangeOnline -eq "Y"){

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

}
$credential = $liveCred
$runningSFB = Read-Host "Are you connecting to Skype for Business Online Powershell? (Y/N)"
if(($runningExchangeOnline.ToLower() -ne "y") -and ($runningSFB.ToLower() -eq "y")){

$credential=get-credential
}
elseif($runningSFB.ToLower() -eq "y"){

$session=new-csonlinesession -Credential $credential 
import-pssession $session
Connect-MsolService -Credential $credential
}

I hope this helps! If you have any questions, comments or suggestions please either use the comments section below, contact me on Twitter @MikeParker365 or via email blog@mikeparker365.co.uk.

Advertisements

3 thoughts on “How To: Connect PowerShell to Exchange Online/Skype for Business Online

  1. Hi Mike,

    I noticed you are using the older MSOnline PowerShell module in your examples. It may be useful to start using the newer Azure Active Directory PowerShell V2 module instead, as we will begin deprecating the MSOnline module when we have migrated the functionality of the MSOnline module to the newer module – currently planned for the Spring of 2017.

    Thanks,

    Rob de Jong

    Like

    1. Hi Rob,
      Thanks for the heads up, I wrote this at the start of 2016 before the Azure Active Directoy Powershell V2 module was released. I might write an updated post in the New Year!
      Thanks,
      Mike

      Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s