Every organization knows that security is essential and needs to be solid. Nowadays, every organization…
How to Connect to Azure AD with PowerShell
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?
It can be overwhelming with all the PowerShell modules that Microsoft releases which let you connect to Azure AD. So, to make it easier, you only need the Microsoft Graph PowerShell module.
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.
PowerShell module prerequisites
Before you start to install the modules, you need to go through the prerequisites below.
1. Start one of the below applications as administrator (high privileges):
- Windows PowerShell
- Windows PowerShell ISE
- Visual Studio Code
2. Set Windows PowerShell Execution Policy
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.
Set-ExecutionPolicy RemoteSigned -Force
Important: Close and re-open the elevated Windows PowerShell window to have the changes apply.
3. Install PowerShellGet module
Run PowerShell as administrator. Run the command Install-Module PowershellGet -Force. When asked to install NuGet provider, press Y and follow with Enter.
Install-Module PowershellGet -Force
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.
1. Microsoft Graph PowerShell module
Microsoft provides the Microsoft Graph PowerShell module to interact with Azure AD. It’s the only module that you need to install and connect to:
2. MSOnline module
Let’s look at how to install, connect, disconnect, update, and uninstall the MSOnline module.
Install MSOnline module
Install the MSOnline module.
Install-Module MSOnline -Force
Connect MSOnline
Run the Connect-MsolService cmdlet to initiate a connection with Azure Active Directory.
Connect-MsolService
After running the above cmdlet, the sign-in window appears.
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()
Get MSOnline module version
Get-Module -ListAvailable MSOnline*
You can keep track of the MSOnline module changelog and its release dates.
Get MSOnline module latest available version
Find-Module MSOnline | ft -AutoSize
Update MSOnline module
Update-Module MSOnline -Force
Uninstall MSOnline module
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.
Get-Module -ListAvailable MSOnline* | Uninstall-Module -Force
3. AzureAD module
Let’s look at how to install, connect, disconnect, update, and uninstall the AzureAD module.
Install AzureAD module
Install-Module AzureAD -Force
Connect AzureAD
Run the Connect-AzureAD cmdlet to initiate a connection with Azure Active Directory.
Connect-AzureAD
After running the above cmdlet, the sign-in window appears.
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
Get AzureAD module version
Get-Module -ListAvailable Az*
You can keep track of the AzureAD module changelog and its release dates.
Get AzureAD module latest available version
Find-Module AzureAD | ft -AutoSize
Update AzureAD module
Update-Module AzureAD -Force
Uninstall AzureAD module
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.
Get-Module -ListAvailable Az* | Uninstall-Module
4. AzureADPreview module
Let’s look at how to install, connect, disconnect, update, and uninstall the AzureADPreview module.
Install AzureADPreview module
Install-Module AzureADPreview -Force
Connect AzureADPreview
Run the Connect-AzureAD cmdlet to initiate a connection with Azure Active Directory.
Connect-AzureAD
After running the above cmdlet, the sign-in window appears.
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
Get AzureADPreview module version
Get-Module -ListAvailable Az*
You can keep track of the AzureADPreview module changelog and its release dates.
Get AzureADPreview module latest available version
Find-Module AzureADPreview | ft -AutoSize
Update AzureADPreview module
Update-Module AzureADPreview -Force
Uninstall AzureADPreview module
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.
Get-Module -ListAvailable Az* | Uninstall-Module
That’s it!
Read more: Install Exchange Online PowerShell module »
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.
This Post Has 0 Comments