Connect to Azure using PowerShell

Do you want to know how to connect Azure portal using PowerShell? Check out this complete tutorial, here, I have explained different ways to connect to Azure using PowerShell.

We can use Azure PowerShell commands to manage resources in Azure.

Connect to Azure using PowerShell

There are two ways to connect to Azure from PowerShell.

  • By using the Install-Module AzureAD command
  • By installing Azure PowerShell on your local machine

We can also use the Azure Cloud Shell to connect to Azure using PowerShell.

Install-Module AzureAD PowerShell Module

To connect to Azure AD using PowerShell, you need first to install the AzureAD PowerShell module.

Make sure to open the PowerShell in admin mode, for this, Type “PowerShell” in the start menu -> Right-click on Windows PowerShell and choose “Run as administrator”

Before installing, you can also check if you have installed it or not by running the below command.

Get-Module -Listavailable

To install AzureAD on your local machine, run the below command in Windows PowerShell. Below is the PowerShell command.

Install-Module AzureAD

Once you run the command, this will install the Azure AD PowerShell module like the screenshot below:

connect to azure powershell

To know what Azure PowerShell modules are installed, run the below command.

Get-InstalledModule
or
Get-Module -Listavailable
powershell connect to azure

It is always good to install the latest version of Azure PowerShell, and you can run the below command to update the Azure PowerShell module.


Update-Module -Name AzureAD

Connect to AzureAD from PowerShell

Once you install the module, run the below PowerShell command to connect to Azure AD.

Connect-AzureAD

You can also run the below command

Connect-AzureAD -Credential (Get-Credential)

Once you run the above command, it will ask you for the username and password (Make sure the account has Azure AD administrator permission), and then it will connect to Azure AD.

Once you connect to Azure AD from PowerShell, you can check out a few examples below.

If you are not sure about the PowerShell commands available in Azure AD, run the below commands.

Get-Command -Module AzureAD

This will list down all the AzureAD PowerShell cmdlets.

Here is an example, Get all users from the Azure AD using PowerShell by running the below command.

Connect-AzureAD
Get-AzADUser

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

connect azure powershell

This is how to connect PowerShell to Azure.

Install Azure PowerShell

Another way to connect to Azure PowerShell is to install Azure PowerShell on Windows using an 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, you will already have the PowerShell 5.1 version.

connect to azure using powershell

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 on the screen it is installing Azure PowerShell.

powershell connect azure

Then, it will show a successful message like the one below:

connect powershell to azure

Click on the Finish button to connect to Microsoft Azure from PowerShell.

Once the installation is over, open Windows PowerShell in Administration mode and run the below command.

Connect-AzAccount

This will ask you to enter the credentials.

The 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, TenantID, and Environment like below:

connect to azure from powershell

This is how to connect Azure portal using PowerShell.

Install AzureADPreview Module

In the azureADPreview module, you will get all the latest updates.

To install AzureADPreview module run the following command.

Install-Module AzureADPreview

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

Below are the useful commands 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

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
powershell command to connect to azure

This is how to install AzureADPreview module to connect to Azure.

Install Azure Active Directory PowerShell Module (MSOnline)

To work with the V1 of Azure AD PowerShell module or known as the MSOnline, you need to use the Connect-MSOLService cmdlet.

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

Install-Module -Name MSOnline

Select Y when it asks 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 powershell

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

Connect-MSOlService

This is how to 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

You can also check out a video I have made on how to connect to Azure AD from PowerShell.

Conclusion

I hope this tutorial will help you to connect to Azure PowerShell. I have explained how to use the Connect-AzureAD to connect to Azure AD from PowerShell. This way you can easily connect Azure portal using PowerShell.

You may also like: