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 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 the Microsoft Azure Active Directory module on your local computer. To check if you have the Azure AD PowerShell module already installed, use the following:

Get-Module AzureAD -ListAvailable

You can also use the “Get-InstalledModule” cmdlet to get a list of installed modules on your local computer.

Assuming you have an x64 bit operating system, at least Windows 7 Sp1/Windows Server 2008 R2 SP1 or later, And have at least a PowerShell version 5.1 installed (Check your current PowerShell version with the command: $PSVersionTable.PSVersion) on your computer, here are the steps to install the AzureAD PowerShell module:

  1. Type “PowerShell” from the start menu >> Right-click on Windows PowerShell and choose “Run as administrator”
  2. Type “Install-Module AzureAD” and hit Enter.
  3. 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 -Name AzureAD

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

Want to suppress the confirmation prompt: You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from ‘PSGallery’? 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 the following command as admin:

Update-Module -Name AzureAD

Step 2: Connect to Azure AD using Connect-AzureAD cmdlet

The next step is to connect to Azure AD from PowerShell. Type Connect-AzureAD cmdlet and hit the enter key. You’ll be prompted to login to Azure AD, which is Multi-factor authentication (MFA) aware. Ensure you have administrator access to Azure Active Directory before executing these cmdlets.

connect to azure ad with powershell

This connects you to Azure Environment and gets you the tenant details such as TenantID, Domain, etc. You can also get the credentials prompt to enter the username and password of an admin account and connect to Azure AD:

Connect-AzureAD -Credential (Get-Credential)

There are more parameters you can pass to the above cmdlet, such as TenantID, AccountID (UserPrincipleName), etc. How about connecting with a saved user name and password?

Import-Module AzureAD

#Parameter
$AdminUserName = "Steve@crescent.com"
$AdminPassword = "Password goes here"
 
#Variable for Pscredential object
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword

#Connect to Azure Active Directory
Connect-AzureAD –Credential $Credential

But the above two methods don’t support two-factor authentication! You can log in to Azure PowerShell using service principal (App ID, Secret/certificate).

Step 3: Start using cmdlets from the Azure AD PowerShell Module.

Once connected, you can start using PowerShell cmdlets available for Azure AD to interact with your tenant. To get all cmdlets from the AzureAD module, use:

#Get Azure AD Tenant Details
Get-AzureADTenantDetail

#Get All Azure AD Users
Get-AzureADUser

#Get All commands from Azure AD PowerShell Module
Get-Command -Module AzureAD

Here, the First command gets Azure AD Tenant Details. The second command will retrieve all Azure AD Users. And the last command will list all PowerShell cmdlets for Azure AD.

powershell cmdlets for azure ad

The Azure AD PowerShell Module allows us to manage users and groups, applications, and domains on Office 365 and Azure with activities such as automating tasks, generate reports, export data, Performing bulk operations, etc. The Azure AD PowerShell module can be installed in client operating systems such as Windows 10 or Server operating systems like Windows 2016.

Once connected, You can start using the cmdlets in your PowerShell script, such as: To list all users in your tenant, use:

Get-AzureADUser | Export-Csv -Path "C:\Temp\AllUsers.csv" -NoTypeInformation

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

Disconnect-AzureAD

Connect to Azure AD using PowerShell without interactive login

For automated scripts, you can use a service principal with a certificate or a secret for non-interactive login.

$tenantId = "your-tenant-id"
$appId = "your-application-id"
$certificateThumbprint = "your-certificate-thumbprint"

#Connect to AzureCloud environment
Connect-AzureAD -TenantId $tenantId -ApplicationId $appId -CertificateThumbprint $certificateThumbprint

How to Install the AzureADPreview module?

The azureADPreview module is where new updates are shipped first. E.g., the cmdlet Get-AzureADAuditSignInLogs is available only in the Azure AD Preview module as of today. To install the preview version of the module, you can replace the module name with AzureADPreview in the Install-Module cmdlet.

Install-Module -Name AzureADPreview

Install Azure AD PowerShell Module V1

For some backward compatibility, If you need the V1 of the Azure AD PowerShell module (AKA: MSOnline), here is how to install and connect to Microsoft Azure Active Directory with Connect-MSOLService cmdlet:

#Install the MSOnline Module
Install-Module -Name MSOnline

#Connect to Azure Active Directory
Connect-MsolService

#Start executing cmdlets
Get-MsolUser

After connecting to Azure AD, you can perform various tasks such as managing users, groups, application registrations, etc.

Wrapping up

And that wraps up the step-by-step process for securely connecting to Azure AD PowerShell and accessing the management capabilities for your tenant. By installing the Azure AD modules, administrators can unlock the full scripting potential of Azure AD through PowerShell automation. PowerShell unlocks efficient and consistent ways to query and modify Azure AD compared to manual methods through graphical tools. Tasks like user license assignments, group administration, and even advanced identity governance activities can be executed through code for added transparency and reliability. PowerShell skills combined with Azure AD’s interfaces pave the way for scalable cloud administration and reduced reliance on GUIs.

Last, but not least: Be sure you set the execution policy (powershell.exe -ExecutionPolicy RemoteSigned), More here PowerShell Execution Policies

What is Azure AD?

Azure Active Directory (Azure AD) is a cloud-based identity and access management service that provides secure access to applications and services.

How do I Connect to Exchange Online with PowerShell?

To connect to Exchange Online with PowerShell, you need to first install the PowerShell Module for Exchange Online Management using “Install-Module ExchangeOnlineManagement”. And then, you can connect to Exchange Online using the Connect-ExchangeOnline cmdlet.
More info: Connect to Exchange Online PowerShell

How do I connect to SharePoint Online from PowerShell?

First, you must download and install the SharePoint Online Management Shell or PowerShell Module. Then you can connect to SharePoint Online through PowerShell using the Connect-SPOService cmdlet.
More info: Connect to SharePoint Online PowerShell

How do I connect to a Microsoft team using PowerShell?

Connecting to teams from PowerShell is a two-step process: First, Install Microsoft Teams PowerShell Module using “Install-Module MicrosoftTeams”. Next, Connect to Microsoft Teams using the PowerShell cmdlet “Connect-MicrosoftTeams”.
More info: Connect to Teams PowerShell

How to connect to Azure AD PowerShell with MFA?

To connect to Azure AD PowerShell with MFA, first, you must install the Azure AD PowerShell module. Then, you can connect to Azure AD PowerShell using the Connect-AzureAD cmdlet. Leave the -Credential parameter to authenticate with MFA. A step-by-step guide can be found in this article!

What is the Tenant ID in Azure AD?

The Tenant ID is a unique identifier for your Azure AD tenant. You can find your Tenant ID in the Azure portal under Azure Active Directory > Properties. More methods here: How to find the Tenant ID in Microsoft 365?

What is the difference between Connect-AzureAD and the Microsoft Graph PowerShell SDK?

The Connect-AzureAD cmdlet is part of the Azure AD PowerShell module designed specifically for managing Azure AD. However, this module is being deprecated for the more versatile Microsoft Graph PowerShell SDK. This SDK allows you to access not only Azure AD but also other Microsoft 365 services like Exchange Online and SharePoint Online.

What permissions do I need to connect to Azure AD in PowerShell?

You need ‘Global Administrator’ or ‘Application Administrator’ role to gain full access to Azure AD in PowerShell. If you are using App ID, The application you register will also need the ‘Directory.ReadWrite.All’ permission.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “How to Connect to Azure AD using PowerShell?

  • Hi, Is there any way of getting to an Azure AD Joined device from here? Lets say to run PS scripts? Trying to enroll aad joined devices to intune that are unreachable physically

    Reply
  • Hi,
    To connect with MFA enabled account, just enter Connect-AzureAD without any switches. You’ll be prompted for logon details and MFA with the usual GUI.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *