Professional Documents
Culture Documents
We like to connect to Azure AD with PowerShell because we want to retrieve Microsoft 365 MFA user
status. Before doing that, we must install a PowerShell module. But which particular PowerShell module
do we need? In this article, you will learn how to connect to Azure AD with PowerShell.
Table of contents
Which PowerShell module to connect to Azure AD?
2. MSOnline module
3. AzureAD module
4. AzureADPreview module
Conclusion
Don’t immediately remove the old modules from your system because there might still be commands
and scripts that use these modules, and you want to convert them first to Microsoft Graph PowerShell.
Important: Azure AD PowerShell is planned for deprecation. You must use Microsoft Graph
PowerShell to interact with Azure AD as you would in Azure AD PowerShell. In addition, Microsoft
Graph PowerShell allows you access to all Microsoft Graph APIs and is available on PowerShell 7.
We will show how to install, connect, disconnect, and update the old modules. Once Microsoft blocks
you from connecting to them, uninstall them from your system.
Before you start to install the modules, you need to go through the prerequisites below.
Windows PowerShell
Windows PowerShell ISE
Visual Studio Code
By default, we can’t install scripts. To require all PowerShell scripts that you download from the internet
are signed by a trusted publisher, run PowerShell as administrator, and run the cmdlet.
Important: Close and re-open the elevated Windows PowerShell window to have the changes
apply.
Run PowerShell as administrator. Run the command Install-Module PowershellGet -Force. When
asked to install NuGet provider, press Y and follow with Enter.
If you get an error that it’s unable to install, read the article Unable to install NuGet provider for
PowerShell.
Now that the prerequisites are set up on the system, let’s look at the PowerShell modules that let you
connect to Azure Active Directory.
2. MSOnline module
Let’s look at how to install, connect, disconnect, update, and uninstall the MSOnline module.
Connect MSOnline
Run the Connect-MsolService cmdlet to initiate a connection with Azure Active Directory.
Connect-MsolService
After connecting with Azure AD, you can run your PowerShell commands.
Disconnect MSOnline
There is no Disconnect-MsolService cmdlet. What you can do is close the Windows PowerShell
window or run the below command.
[Microsoft.Online.Administration.Automation.ConnectMsolService] ::ClearUserSessionState()
You can keep track of the MSOnline module changelog and its release dates.
Close all Windows PowerShell sessions and start a new Windows PowerShell window as administrator
before you run the below command to uninstall the MSOnline module.
3. AzureAD module
Let’s look at how to install, connect, disconnect, update, and uninstall the AzureAD module.
Connect AzureAD
Run the Connect-AzureAD cmdlet to initiate a connection with Azure Active Directory.
Connect-AzureAD
Disconnect AzureAD
Disconnect from the specified Azure AD tenant. This is useful if you want to switch to a different Azure
AD tenant or if you want to disconnect and clear the current authentication session.
Disconnect-AzureAD
You can keep track of the AzureAD module changelog and its release dates.
Close all Windows PowerShell sessions and start a new Windows PowerShell window as administrator
before you run the below command to uninstall the AzureAD module.
4. AzureADPreview module
Let’s look at how to install, connect, disconnect, update, and uninstall the AzureADPreview module.
Connect AzureADPreview
Run the Connect-AzureAD cmdlet to initiate a connection with Azure Active Directory.
Connect-AzureAD
Disconnect AzureADPreview
Disconnect from the specified Azure AD tenant. This is useful if you want to switch to a different Azure
AD tenant or if you want to disconnect and clear the current authentication session.
Disconnect-AzureAD
You can keep track of the AzureADPreview module changelog and its release dates.
Close all Windows PowerShell sessions and start a new Windows PowerShell window as administrator
before you run the below command to uninstall the AzureADPreview module.
That’s it!
Conclusion
You learned how to connect to Azure AD with PowerShell. There are 4 different modules that you can
use. It depends on your scripts and what you want to achieve. That said, Microsoft will deprecate the
modules, and only the Microsoft Graph PowerShell will be available. So, it’s important that you upgrade
all your scripts to Microsoft Graph PowerShell.
Did you enjoy this article? You may also like Install and configure Azure AD Connect. Don’t forget to
follow us and share this article.
Install Microsoft Graph Enable Azure MFA number Get MFA status in Microsoft
PowerShell module matching for extra security Entra and PowerShell
Microsoft Graph PowerShell Every organization knows that You don't want to use PowerShell
replaces the Azure AD PowerShell security is essential and needs to to list Microsoft 365/Microsoft
and MSOnline modules and is be solid. Nowadays, every Entra MFA users status? Instead,…
recommended for… organization…
Leave a Reply
Your email address will not be published. Required fields are marked *
Name * Email *
Comment *
Post Comment