Connect to Office 365 with PowerShell

A brief set of instructions to connect to Office 365 online services using PowerShell, including Azure AD, Exchange Online, and Skype for Business Online.

Install the Microsoft Azure Active Directory Module

Note: it is no longer required to download the Microsoft Online Services Sign-in Assistant for IT Professionals RTW, just run the install-module command below and this will add the PSGallery repository and download from there. You may still want to download the module manually however.

Install the Microsoft Azure Active Directory Module for Windows PowerShell with these steps:
○ Open an administrator-level PowerShell command prompt
○ Run the command:

1
Install-Module MSOnline

Connect to Azure AD

Get your credentials and connect:

1
2
3
$creds = Get-Credential
Connect-MsolService -Credential $creds
Get-MsolDomain (to confirm if you are connected to the right tenant)

Note: If your account is 2FA enabled, just use the command: Connect-MsolService and then enter your credentials and 2FA authentication.

I would also highly recommend changing the window title, especially if you connect to multiple tenants. This reduces the chances of making a change on the wrong tenant!

1
$host.ui.RawUI.WindowTitle = "CustomerX: Production"

Connect to Skype Online

If you also want to manage Skype Online:

Download and install the Skype for Business Online Connector module.

To connect to Skype Online:

1
2
3
4
Import-Module SkypeOnlineConnector
$SkypeSession = New-CsOnlineSession -Credential $creds
Import-PSSession $SkypeSession
Get-CsExternalUserCommunicationPolicy (to test)

Connect to Exchange Online

1
2
3
4
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection
Import-PSSession $ExchangeSession
get-mailbox (to test and verify which tenant you are connected to Skype)
Get-Organizationconfig (useful)

Note if you have MFA enabled: you can either install the Exchange Online PowerShell module, see here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/mfa-connect-to-exchange-online-powershell?view=exchange-ps and then connect using Connect-EXOPSSession -UserPrincipalName user@domain.com instead of the above, or you can use the method outline here to connect using the normal PowerShell console or ISE: https://cloudrun.co.uk/powershell/connecting-to-exchange-online-using-mfa-without-exo-powershell-module/.

 

Connecting through a proxy server

Option 1: Use netsh

You can just a single command to set a proxy: netsh winhttp set proxy. This is persistent, and I prefer this because it will work with either connection method for Exchange – creating a remote session or using the Exchange Online PowerShell module with MFA.

1
2
3
netsh winhttp show proxy #Check proxy settings
netsh winhttp set proxy 192.168.5.6:8118 #Set proxy server
netsh winhttp reset proxy #Remove proxy settings

Option 2: Set your proxy per session

If you need to use your proxy with remote PSSessions, connect as follows:

1
2
3
$proxyOptions = New-PSSessionOption -ProxyAccessType IEConfig
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection -SessionOption $proxyOptions
Import-PSSession $Session

Note that if your proxy requires authentication, you can use this third option: https://cloudrun.co.uk/powershell/configuring-powershell-to-work-behind-a-proxy-server/

Connect to the Security& Compliance center

1
2
3
4
5
6
7
$creds = get-credential
$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $creds -Authentication "Basic" -AllowRedirection
Import-PSSession $SccSession -Prefix cc

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $creds -Authentication Basic –AllowRedirection
Import-PSSession $Session
Get-RetentionCompliancePolicy (to test)

See https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-office-365-powershell and https://docs.microsoft.com/en-us/office365/enterprise/powershell/manage-skype-for-business-online-with-office-365-powershell for more information.

Also https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-all-office-365-services-in-a-single-windows-powershell-window

Posted in Office 365, PowerShell

Related Posts

5 Comments

  1. Pingback:Configuring PowerShell to work behind a proxy server – Cloudrun

  2. Pingback:Configuring PowerShell to work behind an authenticated proxy server - Cloudrun

  3. Pingback:Configure Exchange Online archiving for all users - Cloudrun

  4. Pingback:Exchange Online Enable Archive Mailboxes and Archive Policies for all users - Cloudrun

  5. Pingback:Connecting to Exchange Online using MFA without EXO PowerShell Module - Cloudrun

Leave a Reply

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

%d bloggers like this: