Earlier, we have blogged about connecting to Exchange Online using PowerShell. In continuation to that, this blog describes the steps to connect to Office 365 via PowerShell.
Let’s jump into it.
There are two types of modules with which you can connect to Microsoft 365/Office 365 using PowerShell.
1.AzureAD V1 (MSOnline Module) – Microsoft Azure Active Directory Module for Windows PowerShell
2.AzureAD V2 (AzureAD Module) – Azure Active Directory PowerShell for Graph
Since the Azure AD V2 module does not replace all the functionalities provided by the Azure AD V1 module, most admins use both modules. We recommend installing both modules in your system to manage your Office 365 environment effectively.
You can install the MSOnline PowerShell module using the below cmdlet in the Windows PowerShell.
Install-Module –Name MSOnline
Cmdlets in the MSOnline module have ‘Msol’ in their cmdlet prefix like Get-MsolUser, Get-MsolAccountSku, etc.
To install the Azure AD PowerShell module, run the following cmdlet in the Windows PowerShell.
Cmdlets in the Azure Active Directory module have ‘AzureAD’ in their cmdlet name, such as Get-AzureADUser, New-AzureADGroup, etc.
Admins can connect to Office 365 using both MSOnline and AzureAD modules at a time. Let’s see both modules individually.
You can use the ‘Connect-MsolService’ cmdlet to connect to Office 365 PowerShell. You can also pass the PSCredential object by using the Credential param.
$Credential = Get-Credential Connect-MsolService –Credential $Credential
If you want to connect to Office 365 with an MFA enabled account, you can directly use the below cmdlet alone.
It will prompt you to enter your username and password. You can also use the above format to connect to Office 365 with the non-MFA account.
Note: To avoid the ‘Connect-MsolService is not recognized as the name of a cmdlet’ error, you must import the MSOnline module cmdlets using the following cmdlet.
The ‘Connect-AzureAD’ cmdlet is used to connect to Azure Active Directory. If you want to supply credentials in advance, you can use the Get-Credential cmdlet.
$Credential=Get-Credential Connect-AzureAD –Credential $Credential
To connect to Azure AD PowerShell with an MFA account, you can use the below format.
Since ‘Connect-AzureAD’ cmdlets can directly prompt for username and password, you can use it for both MFA and non-MFA accounts.
Note: You must import Azure AD PowerShell cmdlets before connecting to Office 365. Else, you will receive the following error: Connect-AzureAD is not recognized as the name of a cmdlet.
To import the AzureAD PowerShell module, run the below cmdlet.
If you are using multiple Microsoft 365 services like Azure AD, Exchange Online, SharePoint, Teams, etc., we’d recommend you download the script to automatically install and connect to all Office 365 PowerShell modules.
I hope this blog will help you to connect to your Office 365 PowerShell with ease. See you soon in the next blog.