Connect PowerShell remotely to M365 service

Scenario

Are you looking to remotely connect to your M365 service? If so, you are in luck because there are a number of ways that you can do this. In this article, we will look at a method: using PowerShell.

M365 is Microsoft’s enterprise cloud-based office 365 service. It offers users the ability to access their mailbox, contact lists, files, and calendars from any device that has an internet connection. M365 also offers secure authentication and authorization services.

With the recent proliferation of cloud-based services, connecting to them remotely has become a common task. PowerShell offers several advantages when it comes to working with remote systems. For example, it is scriptable and lightweight, making it easy to automate tasks. Additionally, PowerShell makes it easy to connect to cloud-based systems using browser-based tools such as Azure Active Directory Connector for Azure AD.

Steps

This section will describe how to connect to each specific Microsoft 365 services. Firstly, you need to have the following below to succesfully configure the Microsoft 365 connection via PowerShell.

  • Windows machine with administrator privellage to install the required module
  • An open internet access (none VPN) to bypass any network restrictions
  • An specifi admin roles against the target service or better yet a Global Administrator role for your user account.
  • Type and run Set-ExecutionPolicy RemoteSigned on your PowerShell

 

Connecting to Exchange Online (EXO)

Classic (non-MFA)

Set-ExecutionPolicy RemoteSigned

$credential = Get-Credential

$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection

Import-PSSession $exchangeSession -DisableNameChecking

Exchange (with Proxy)

#install module
Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement

#setup proxy
$ProxyOptions = New-PSSessionOption -ProxyAccessType IEConfig

Connect-ExchangeOnline -PSSessionOption  $ProxyOptions 

Exchange (v2)

#install module
Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement

Connect-ExchangeOnline

#disconnect session
Disconnect-ExchangeOnline

Connecting to Azure Active Directory (MSOnline)

Classic

1. Install module and connect

# one time config
Install-Module MSOnline
Import-Module MSOnline

# connect
Connect-MSolservice

m365-connect-powershell-img-1

Connecting to SharePoint Online (SPO)

#install module
Install-Module -Name Microsoft.Online.SharePoint.PowerShell

#connect to your spo admin url
Connect-SPOService -Url https://faceresionem-admin.sharepoint.com

#"faceresionem" is my intial alias for my "onmicrosoft.com" domain upon signing up with M365

#disconnect session
Disconnect-SPOService 

More on “onmicrosoft.com” domain

Connecting to SharePoint Online (PnP)

# one time config
Install-Module SharePointPnPPowerShellOnline
Import-Module SharePointPnPPowerShellOnline

#connect
Connect-PnPOnline -Url https://faceresionem.sharepoint.com

#"faceresionem" is my intial alias for my "onmicrosoft.com" domain upon signing up with M365

# you can also connect to a site collection level

Connect-PnPOnline -Url https://faceresionem.sharepoint.com/sites/mySiteCollectName

Connecting to Azure Active Directory (AzureAD)

#install module
Install-Module -Name AzureAD

Connect-AzureAD

#disconnect session
Disconnect-AzureAD

Connecting to Security & Compliance Center (SecCom)

MFA

#install module
Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement

Connect-IPPSSession

#disconnect session
Disconnect-ExchangeOnline

Classic (non-MFA)

Set-ExecutionPolicy RemoteSigned

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session -DisableNameChecking

Connecting to Skype For Business (S4BO)

1. Install Skype for Business Online, Windows PowerShell Module

2. Run below cmdlets according to situation

LyncOnlineConnector (non-MFA)

Set-ExecutionPolicy RemoteSigned

Import-Module LyncOnlineConnector 

$lyncSession = New-CsOnlineSession -Credential $credential

Import-PSSession $lyncSession

SkypeOnlineConnector (non-MFA)

Set-ExecutionPolicy RemoteSigned

Import-Module SkypeOnlineConnector

$userCredential = Get-Credential

$sfbSession = New-CsOnlineSession -Credential $userCredential

Import-PSSession $sfbSession

Lync discover issue (error bypass)

 Import-Module LyncOnlineConnector

$userCredential = Get-Credential

$cssession = New-CsOnlineSession Credential $cred OverrideAdminDomain "fresheggs.onmicrosoft.com" 

Import-PSSession $cssession

Connecting to Teams (MSTeams)

References