How to Connect to Azure in PowerShell (And Azure AD)

In this Azure AD tutorial, we will discuss how to connect to azure in PowerShell. We will also see how to install the Azure Active Directory PowerShell Module to work with Azure AD using PowerShell.

How to Connect to Azure in PowerShell (And Azure AD)

Well, let’s discuss how to Connect to Azure in PowerShell (And Azure AD)

Also, you may like following Azure tutorials:

We will see two ways to connect to Azure using PowerShell.

Connect to Azure and Azure AD from PowerShell – Video Tutorial

Subscribe to Our YouTube Channel for more videos

Connect to Azure PowerShell

To connect to Azure PowerShell, first we need to install Azure PowerShell on Windows using MSI installer.

To work with Azure PowerShell, you should have:

Note: The PowerShell MSI installer works on having PowerShell versions 5.1 or higher. You can check the current PowerShell version by using the below command:

$PSVersionTable.PSVersion

If you are using the Widows 10 OS, then you will be having already the PowerShell 5.1 version.

get powershell version

Now, you can download Azure PowerShell MSI package from GitHub.

Once downloaded, run the setup to install Microsoft Azure PowerShell. Click on Install like below:

connect to azure powershell

Now, you can see in the screen, it is installing Azure PowerShell.

install microsoft azure powershell

Then it will show a successful message like below:

How to Connect to Azure in PowerShell (And Azure AD)

Click on the Finish button and then you can connect to Microsoft Azure from PowerShell.

Open Windows PowerShell in Administration mode and run the below command.

Connect-AzAccount

We can use Connect-AzAccount command to connect to Microsoft Azure from PowerShell.

Connect-AzAccount Powershell Azure

Once you enter the credentials, it will display the Azure details like Account, subscriptionname, tenant id, environment like below:

connect to azure from powershell

This is how we can connect to Microsoft Azure from PowerShell.

Install Azure Active Directory PowerShell Module in Windows 10 (AzureAD)

Now, we will see how to install Azure active directory PowerShell Module in Windows 10.

Open PowerShell in Admin mode like below:

Install Azure Active Directory PowerShell Module in Windows 10

Then run the below command:

Install-Module AzureAD

This will install the Azure AD PowerShell module like below:

Install Azure Active Directory PowerShell Module in Windows 10

You can also check what are the modules installed by running the below command.

Get-InstalledModule
or
Get-Module -Listavailable
Install Azure AD PowerShell Module

Then you can run the below command to connect to Azure AD.

Connect-AzureAD

Once you run the command, it will ask you the user name and password (Azure AD administrator) and then it will connect to Azure AD.

Then you can retrieve all users from the Azure AD using PowerShell by running the below command. (You can add the code in Windows PowerShell ISE)

Connect-AzureAD
Get-AzADUser

You can see it will display all the users from the azure active directory.

get azure ad users powershell

Optional Content

Once you install this, then you will see few dlls will be there in the below folder:

C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.76

Here 2.0.2.76 is the version, may be varies depending on the version you are installing.

Here find the Microsoft.Open.AzureAD16.Graph.Client.dll

Here, we need to add the above dlls references.

The full code will looks like below: (You can add the code in Windows PowerShell ISE)

Here I am retrieving get Azure AD users using PowerShell.

Add-Type -Path 'C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.76\Microsoft.Open.AzureAD16.Graph.Client.dll'
Connect-AzureAD
Get-AzADUser

You can see the Azure active directory users like below:

get azure ad user powershell

Install Azure AD PowerShell for Graph module (AzureADPreview)

We can install Azure AD PowerShell for Graph module which is known as AzureADPreview.

If you have already installed AzureAD, then you can uninstall AzureAD before installalling AzureADPreview.

Below are the useful command to work with AzureADPreview.

Check which AzureAD module in installed

You can run the below command to check which AzureAD module has been installed in the system.

Get-Module -ListAvailable AzureAD*

Uninstall AzureAD module

If you have already installed AzureAD module, then uninstall the AzureAD module before installing AzureADPreview module.

Uninstall-Module AzureAD

Install AzureADPreview module

You can run the below command to install AzureADPreview module.

Install-Module AzureADPreview

Install the latest version of AzureADPreview  module

Anytime, you can uninstall the AzureADPreview  module and install the latest version of the AzureADPreview  module by running the below command.

Uninstall-Module AzureADPreview
Install-Module AzureADPreview
Install AzureADPreview module

This is how we can install AzureADPreview PowerShell for Graph module.

Install Azure Active Directory PowerShell Module (MSOnline)

Now, we will see how to install the Azure Active Directory PowerShell Module. So that you can work with Azure Active directory from PowerShell.

You can also manage users or organization’s information in Office 365 via PowerShell.

Open Windows PowerShell and Run as administrator.

Note: You can also use Windows PowerShell ISE.

Install the Azure Active Directory PowerShell Module via PowerShell

Then run the below command which will install the Azure Active Directory PowerShell Module:

Install-Module -Name MSOnline

Select Y, when it ask for NuGet provider is required to continue. And again press Y for the trusted provider.

Finally, you can see the Azure AD PowerShell module will be installed like below:

Connect to Azure Active Directory PowerShell

Once installed successfully, you can connect to Azure AD from PowerShell by running the below command:

Connect-MSOlService
install Azure Active Directory PowerShell Module

This is how you can connect to Azure AD from PowerShell.

Then you can run the below command to get all the users from the Azure active directory.

Get-AzADUser

In this tutorial, we discuss how to Microsoft Azure from PowerShell. Also we saw:

  • Install Azure Active Directory PowerShell Module in Windows 10
  • Install Azure Active Directory PowerShell Module

Leave a Comment