How to Connect to Azure AD using PowerShell?

Requirement: Connect to Azure AD with PowerShell

How to Connect to Azure Active Directory using PowerShell?

Azure Active Directory (Azure AD) is Microsoft’s cloud-based identity and access management service. Azure AD allows you to manage user identities and their access rights to your applications, whether on-premises or in the cloud. With the Power of PowerShell, we can automate tasks, access settings that are not available in the web user interface, Filter and query data, generate reports, make configuration changes to the objects, etc. This blog post will show you how to connect to Azure AD using PowerShell and demonstrate some of the basics of working with Azure AD for Office 365 using PowerShell!

Step 1: Install the Azure AD PowerShell Module

To start with Azure AD PowerShell, You have to install it on your local computer. To check if you have the Azure AD PowerShell module already installed, use:

Get-Module AzureAD -ListAvailable

Assuming you have x64 bit operating system at least Windows 7 Sp1/Windows 2008 R2 SP1 or later, here are the steps to install the Azure AD PowerShell module:

  • Type “PowerShell” from the start menu >> Right-click on Windows PowerShell and choose “Run as administrator”
  • Type “Install-Module AzureAD” and hit Enter.
  • You’ll be asked to confirm the installation from the PSGallery. Type “A” to select “Yes to All” and hit the Enter key.install azure ad powershell module
Install-Module AzureAD

This will download and install the PowerShell module for Azure Active Directory to your local computer. (AKA: Azure Active Directory PowerShell for Graph)

It’s a good idea to trust PowerShell Gallery, so that you won’t get this confirmation prompt! Use: Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

To update the existing Azure AD module to the latest version, run this command:

Update-Module -Name AzureAD

Step 2: Connect to Azure AD

The next step is to connect to Azure AD from PowerShell. Type “Connect-AzureAD” and hit the enter key. You’ll be prompted to log in to Azure AD, which is MFA aware. Ensure you have administrator access to Azure Active Directory before executing these cmdlets.

connect to azure ad with powershell

You can also get the credentials and connect to Azure AD, as:

Connect-AzureAD -Credential (Get-Credential)

But the above method doesn’t support two-factor authentication!

Step 3: Start using cmdlets from Azure AD PowerShell Module

Once connected, you can start using PowerShell cmdlets available for Azure AD. To get all cmdlets from the Azure AD module, use:

Get-Command -Module AzureAD

This will list all PowerShell cmdlets for Azure AD

powershell cmdlets for azure ad

You can start using the cmdlets, such as:

Get-AzureADUser

To disconnect from Azure in your PowerShell session, run the command below:

Disconnect-AzureAD

Install Azure AD PowerShell Module V1

For some backward compatibility, If you need the V1 of the Azure AD PowerShell module (AKA: MSOnline Module), here is how to install and connect to Azure AD:

#Install the MSOnline PowerShell Module
Install-Module MSOnline

#Connect to Azure Active Directory
Connect-MsolService

#Start executing cmdlets
Get-MsolUser

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply