Thursday, May 6, 2021
Home Office 365 PowerShell Connect to Office 365 via PowerShell (MFA and Non-MFA)

Connect to Office 365 via PowerShell (MFA and Non-MFA)

To perform a large number of tasks, especially custom automated tasks, it is necessary to connect to the Office 365 tenant with Powershell. If you are familiar with Powershell in Exchange then the commands are very similar. Clearly there are additional cmdlets that are relevant to Office 365 and in other articles we can explore those.

As I promised, there are changes here to take into effect the requirement to support MFA accounts. I have edited the article now to take into effect these updates from Microsoft. The link from Microsoft can be found here.

https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/exchange-online-powershell-v2/exchange-online-powershell-v2?view=exchange-ps

The main function however is to get the connection running in the first place. To do this you need to start up ‘Windows Powershell ISE’ from your desktop and Run As Administrator. The running as administrator is very important as it will not connect if you don’t.

As a pre-requisite to this you will need to install the Microsoft Office 365 Online Sign In Assistant. You can find the executable https://www.microsoft.com/en-us/download/details.aspx?id=41950.

Also the Azure AD modules can be downloaded here https://docs.microsoft.com/en-us/powershell/azure/active-directory/install-adv2?view=azureadps-2.0. The short version of these instructions is to simply type the following command in the Powershell console

install-module msolonline
install-module azureadpreview

To import these modules into your Powershell sessions after you have installed them, type these commands in the Powershell console.

import-module msonline
import-module azureadpreview

If this is the very first time you have run this type of script on your machine, you will need to start by executing this command as a ‘one-off’.

Set-ExecutionPolicy RemoteSigned

The new need here now is to import and install the new Exchange Online Management cmdlets. This can be done with the following commands.

Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement; Get-Module ExchangeOnlineManagement
Update-Module -Name ExchangeOnlineManagement

After this point on your machine you will be able to run any kind of script via the Powershell ISE. Then you can use the following script to perform the connection.

Set-ExecutionPolicy RemoteSigned

#Setup Username and Credentials for non-MFA Accounts
$username = “xxxxx@xxxxxx.onmicrosoft.com”
$password = ConvertTo-SecureString “xxxxxx” -AsPlainText -Force
$UserCredential = new-object -typename System.Management.Automation.PSCredential -ArgumentList $username, $password

#Setup Username if an MFA Account
$mfauser = "xxxxx@xxxxxx.onmicrosoft.com"

#If User is Non-MFA use this connection
Connect-ExchangeOnline -Credential $UserCredential -ShowProgress $true

#If user is MFA use this connection
Connect-ExchangeOnline -UserPrincipalName <UPN> -ShowProgress $true

import-module msonline
connect-MsolService -Credential $UserCredential
import-module azureadpreview
connect-AzureAD -Credential $UserCredential

Replace the items with X’s with the credentials for your tenant. The end result will look like this.

From here you can use normal Exchange style powershell to perform functions on Office 365. 

Mark Rochesterhttps://exchange2019.com
Mark currently works in the cloud space assisting large companies to migrate from either on premises to the cloud, or cloud to cloud. His experience with Enterprise migrations spans more than 25 years which basically makes him old. However, with all the oldness creeping up he still finds technology massively exciting. Please reach out for a chat anytime you would like. 🙂

Most Popular

Recent Comments