The Connect-MsolService cmdlet in PowerShell is used to initiate a connection to the Microsoft Azure Active Directory. The Connect-MsolService cmdlet is necessary for running most other Microsoft Office 365 PowerShell cmdlets.
Azure Active Directory is a cloud-based identity that allows administrators to manage users, groups, and applications across multiple cloud applications.
The Connect-MsolService cmdlet can be used to manage a variety of Office 365 services, including Exchange Online, SharePoint Online, and OneDrive for Business.
In this article, we will discuss how to use the Connect-MsolService cmdlet in PowerShell to connect to Microsoft Office 365.
The syntax for the Connect-MsolService cmdlet is given below.
Connect-MsolService [-Credential <PSCredential>] [-AzureEnvironment <AzureEnvironment>] [-AdGraphAccessToken <String>] [-MsGraphAccessToken <String>] [<CommonParameters>]
How to Connect to Msol Service
The Connect-MsolService cmdlet attempts to initiate a connection with Azure Active Directory. You must specify the credentials, as a PSCredential object or specify the CurrentCredentials parameter to use the credentials of the current user.
The following is an example of how to use the Connect-MsolService command to connect to Office 365.
In the above PowerShell script, the Connect-MsolService cmdlet attempts to initiate a connection with Azure Active Directory. Since no credentials are provided, this command prompts you to enter the username and password.
Once you have entered your credentials, you will be connected to Office 365. You can then use the MSOnline module cmdlets to configure and manage Office 365 services.
How to Connect to Azure AD with a Credential Object
To connect to Azure Active Directory with a credential object, use the Connect-MsolService cmdlet with the
Credential parameter. The Credential parameter specifies the credential to use to connect to the Azure Active Directory.
# Get the Credentials to connect to Office 365 $Cred = Get-Credential # Initiate a connection by using a credential object Connect-MsolService -Credential $Cred
In the above PowerShell script, the
Get-Credential command is used to obtain a PSCredential object and store it in the
$Cred variable. The
Connect-MsolService command uses the
-Credential parameter to specify the credential object to connect to Azure Active Directory and its services.
The Connect-MsolService cmdlet uses the below parameters to connect to Azure AD.
-Credential: It specifies the credential to use to connect to Azure AD. Use the
Get-Credential cmdlet to get the PSCredential object.
-AdGraphAccessToken: It specifies the AD Graph access token to use to connect to Azure Active Directory.
-MSGraphAccessToken: It specifies the MS Graph token to use to connect to Azure Active Directory.
-AzureEnvironment: It specifies the deployment type to use to connect Azure AD in different regions. Valid values are:
How to Fix the Term “Connect-MSolService is not recognized”
While trying to connect with Office 365 with PowerShell, if you receive the following error message, it means the MSOnline module is not installed.
Connect-MsolService : The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
To fix the term “‘Connect-MsolService’ is not recognized” issue, follow these steps:
Step 1: Open the PowerShell ( Run as Administrator).
Step 2: Install the MSOnline PowerShell module.
Install-Module -Name MSOnline
If you are prompted to install the NuGet provider, type Y and press enter. Click Yes or type Y to install the module from an untrusted repository.
Step 3: Import the MSOnline PowerShell Module
This command will import the MSOnline module in your current PowerShell session.
Step 4: Verify the MSOnline module available
To verify the MSOnline module installed, run the following command.
Get-Command -Module MSOnline
Get-Command command returns all cmdlets available in the MSOnline module.
Step 5: Connect to Office 365 using the Connect-MsolService cmdlet
Connect-MsolService command to connect to Office 365, and enter the global admin and password to sign in.
Cool Tip: How to disconnect from MsolService in PowerShell!
I hope the above article on the Connect-MsolService cmdlet to connect to Azure Active Directory is helpful to you.
Once you have connected to Microsoft 365 or Azure Active Directory, you can start running other Microsoft 365 PowerShell cmdlets, such as Get-MsolUser cmdlet to retrieve a list of all users in your tenant.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.