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.

Environment

We recommend using PowerShell 5.x rather than the newer PowerShell 7.x, which is based on .NET Core. Although 7.x is great because it is cross platform, some modules you may need (e.g. SharePoint Online Management Shell or SPOService), will not work with 7.x.

So you will need to use a Windows client with the PowerShell 5.x installed, which is included with any version newer than Windows 10 or Server 2016 or later, or can be installed on earlier versions, see Windows PowerShell System Requirements – PowerShell | Microsoft Docs.

Connect to Azure AD v1 (msonline)

This is the older MSOnline V1 PowerShell module for Azure Active Directory.

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

Install-Module MSOnline

This is the older MSOnline V1 PowerShell module for Azure Active Directory.

Get your credentials and connect:

$creds = Get-Credential
Connect-MsolService -Credential $creds
Get-MsolDomain # 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!

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

Connect to Azure AD v2

Install-Module AzureAD
Connect-AzureAD

Note that the login window is often hidden behind the console, so minimise to see it.

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:

Import-Module SkypeOnlineConnector
$SkypeSession = New-CsOnlineSession -Credential $creds
Import-PSSession $SkypeSession
Get-CsExternalUserCommunicationPolicy # To test

Connect to Exchange Online

This section has been updated for the new Exchange Online PowerShell V2 module module, this works with MFA out of the box, and will also work with all the v1 commands.

Install-Module ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnlineGet-Organizationconfig # Useful to test

Connect to SharePoint Online

The SharePoint Online Management Shell is a Windows PowerShell module that you can use to manage SharePoint settings at the organization level and site collection level.

Install the module:

Install-Module -Name Microsoft.Online.SharePoint.PowerShell

And connect:

Connect-SPOService -Url https://TenantName-admin.sharepoint.com

Connect to SharePoint PnP

PnP PowerShell is a cross-platform PowerShell Module providing over 500 cmdlets that work with Microsoft 365 environments and more specifically SharePoint Online, Microsoft Teams, Microsoft Planner and Microsoft Flow.

Install-Module -Name PnP.PowerShell
$adminURL = "https://tenantname-admin.sharepoint.com/"          
Connect-PnPOnline -Url $adminURL -Interactive

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.

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:

$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

$creds = get-credential
$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

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.