skip to Main Content

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 we can do that, we have to install Microsoft Azure Active Directory Module. In this article, you will learn how to install Azure Active Directory module.

Install MSOnline module

To install the MSOnline module, you need to start one of the below applications as administrator (high privileges):

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. Press Y and press Enter.

PS C:\> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y

Important: Close and re-open the elevated Windows PowerShell window to have the changes apply.

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.

PS C:\> Install-Module PowershellGet -Force

NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\administrator.EXOIP\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install
and import the NuGet provider now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y

If you get an error that it’s unable to install, read the article Unable to install NuGet provider for PowerShell.

Install Azure Active Directory module

Install the MSOnline module. Press Y and Enter.

PS C:\> Install-Module MSOnline

Connect to Azure Active Directory

Run the Connect-MsolService cmdlet to initiate a connection with Azure Active Directory.

PS C:\> Connect-MsolService

After running the above cmdlet, the sign-in window shows up.

Connect to Azure AD with PowerShell

After connecting with Azure AD, you can run your PowerShell commands.

Note: There is no Disconnect-MsolService cmdlet. What you can do is close the Windows PowerShell window.

Read more: Install Exchange Online PowerShell V2 »

Conclusion

You learned how to connect to Azure AD with PowerShell. Before you connect with MsolService (MSOnline), install the MSOnline module. Now you’re ready to run your commands and scripts.

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.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 0 Comments

Leave a Reply

Your email address will not be published.