
Connect to Office 365 PowerShell
Earlier, we have blogged aboutconnectingtoExchange Onlineusing PowerShell.In continuationto that, this blog describes the steps to connect to Office 365 via PowerShell.
Let’s jump into it.
There are two types of moduleswith which you can connect toMicrosoft 365/Office 365 using PowerShell.
1.AzureAD V1 (MSOnlineModule) – Microsoft Azure Active Directory Module for Windows PowerShell
2.AzureAD V2 (AzureADModule) – Azure Active Directory PowerShell for Graph
Since the Azure AD V2 moduledoes 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 theMSOnlinePowerShell module using the belowcmdletin the Windows PowerShell.
Install-Module –NameMSOnline
Cmdlets in theMSOnlinemodule have ‘Msol’ in their cmdlet prefix likeGet-MsolUser, Get-MsolAccountSku, etc.
To install the Azure AD PowerShell module, run the following cmdlet in the Windows PowerShell.
Install-ModuleAzureAD
Cmdlets in the Azure Active Directory module have ‘AzureAD’ in their cmdlet name, such asGet-AzureADUser, New-AzureADGroup, etc.
Admins can connect to Office 365 using bothMSOnlineandAzureADmodules 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 thePSCredentialobject 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.
Connect-MsolService
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-MsolServiceis not recognized as the name of a cmdlet’ error, you must import theMSOnlinemodule cmdlets using the following cmdlet.
Import-ModuleMSOnline
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.
For example
$Credential=Get-Credential Connect-AzureAD–Credential $Credential
To connect to Azure AD PowerShell with an MFA account, you can use the below format.
Connect-AzureAD
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-AzureADis not recognized as the name of a cmdlet.
To import theAzureADPowerShell module, run the below cmdlet.
Import-ModuleAzureAD
If you are using multiple Microsoft 365 services likeAzureAD, Exchange Online, SharePoint, Teams, etc., we’d recommend you download the script to automaticallyinstall and connect to all Office 365 PowerShell modules.
Note: For security reasons, if you want to block users from accessing Azure AD PowerShell, you can refer to the guide on ‘Restrict user access to Azure AD PowerShell’.
I hope this blog will help you to connecttoyourOffice 365PowerShellwithease.See you soon in the next blog.