Connect Azaccount Cmdlet Explained With Examples


To make this article easy to read, I have divided them into sections, starting with an overview of this cmdlet. Moreover, before you can use the Connect-AzAccount cmdlet, you need to install the Az.Accounts PowerShell module. So, in the second section, I’ll show you how to install the Az.Accounts PowerShell module. Once you have this module on your computer, you can proceed to read the syntaxes and parameters of the Connect-AzAccount cmdlet.

Overview Of The Connect-AzAccount Cmdlet

As I hinted in my introduction, the Connect-AzAccount cmdlet is part of the Az.Accounts PowerShell module. So, if you try to run this command without installing this module, you’ll receive an error message – see the screenshot below.

Meanwhile, this cmdlet connects you to an Azure tenant with an authenticated account. Once you connect to Azure with the Connect-AzAccount cmdlet, you can use the other cmdlets in the Az PowerShell module. In the last paragraph, I mentioned that you need an authenticated account to use Connect-AzAccount to connect to Azure. Based on this, it is recommended to use the Get-Credential command to save your authenticated credentials in a variable. Then, use the -Credential parameter of the Connect-AzAccount cmdlet to connect to your Azure tenant. Before I move on from this overview section, I’ll also like to mention that the Connect-AzAccount cmdlet has two other aliases – Login-AzAccount and Add-AzAccount. So this means that you can use Login-AzAccount or Add-AzAccount instead of Connect-AzAccount. With the basics out of the way, let’s now move on to the juicy parts of this article!

How To Install The Az.Accounts PowerShell Module

I have already mentioned more than once in this article that you need to install the Az.Accounts PowerShell module before you can use the Connect-AzAccount cmdlet. Follow the steps below to install the Az.Accounts PowerShell module. When PowerShell finishes installing the module, when you run the Connect-AzAccount command, PowerShell will prompt you for your credentials. However, before we start playing around with this cmdlet, let’s learn its syntaxes and parameters first. I will cover these in the next two sections. Here is the screenshot of the result of the command. I have highlighted the part of the result that shows that Login-AzAccount and Add-AzAccount are the aliases of Connect-AzAccount.

Syntaxes Of The Connect-AzAccount Cmdlet

The Connect-AzAccount cmdlet has seven syntaxes. Here they are… The first syntax of the Connect-AzAccount cmdlet is the basic syntax with one unique parameter – UseDeviceAuthentication. What differentiates the first from the second syntax is the presence of Credential and ServicePrincipal parameters in the second syntax. However, it is important to mention that the second syntax does not include the UseDeviceAuthentication parameter. Moving on to the third syntax, this syntax is essentially different from the first and second syntaxes. Specifically, the third syntax does not include the Credential, but it includes the ServicePrincipal parameter. However, it includes three new parameters not found in the first two syntaxes – ApplicationId, SendCertificateChain, and CertificateThumbprint. Like the third parameter, the fourth syntax also includes the ApplicationId, SendCertificateChain, and ServicePrincipal parameters. In addition to these three parameters shared with the third syntax, this syntax has two more unique parameters – CertificatePath and CertificatePassword. That brings us to the fifth syntax of the Connect-AzAccount cmdlet. This syntax shares the ApplicationId and ServicePrincipal parameters with the third and fought parameters. However, the fifth syntax has one parameter unique to it – FederatedToken. As you may have noted, the third, fought, and fifth syntaxes of the Connect-AzAccount cmdlet share some common parameters. However, the sixth and seventh syntaxes are unique, with no parameter common to the rest syntaxes. Specifically, the sixth has five unique parameters – AccessToken, AccountId, KeyVaultAccessToken, GraphAccessToken, and MicrosoftGraphAccessToken. Finally, the seventh syntax of the Connect-AzAccount cmdlet also has AccountId but includes a unique Identity parameter. To make it easier to understand the differences in the syntaxes, I have summarised them in the table below:

Parameters Of The Connect-AzAccount Cmdlet Explained

In the last section, I listed and explained the seven syntaxes of the Connect-AzAccount cmdlet. In the table below, I have explained the parameters that make up the syntaxes of the command.

Applications And Examples Of The Connect-AzAccount Cmdlet

Now that you’ve some information about the Connect-AzAccount cmdlet, it is time to dive into some applications and examples. In the following sub-sections of this section, I have discussed some examples and applications of this Azure cmdlet.

How To Fix The Connect-AzAccount Not Recognized Error

In the overview section of this article, I mentioned that if you run the Connect-AzAccount command without installing the Az.Accounts PowerShell module you will receive the “Connect-AzAccount Not recognized” error. Based on this, earlier in this article, I discussed How To Install The Az.Accounts PowerShell Module. So, the reason you receive the “Connect-AzAccount Not recognized” error is that you’ve not installed the Az.Accounts PowerShell module. To fix this error and run the Connect-AzAccount command successfully, open powershell as administrator. Then, run the command below: Once you’ve installed this module, you can run the Connect-AzAccount command without receiving the “Connect-AzAccount Not recognized” error.

How To Avoid Azure Browser Authentication When You Run Connect-AzAccount

If you run the Connect-AzAccount command without specifying the Credential parameter, PowerShell will open a login authentication link on your default browser. To avoid this happening, you must specify the Credential parameter in your command. To connect to your Azure tenant and avoid Azure opening a browser for authentication, use the following commands. When I ran the last command in my script, I received the “You must use multi-factor authentication to access tenant xxx” error message. To fix this problem, you need to turn off “Enable security defaults” in your Azure portal. See the next subsection for the steps to fix this error. Once you have turned off “Enable security defaults” in your Azure portal, re-run the commands below and you should be able to connect to Azure with Connect-AzAccount successfully. Here are the results of the commands in my above script. As you can see, because I included the Credential parameter to the Connect-AzAccount command, PowerShell did not need to open a browser to request authentication.

How To Fix The Connect-AzAccount Commmand’s “You must use multi-factor authentication to access tenant” Error

To fix the “You must use multi-factor authentication to access tenant” Connect-AzAccount error, you must turn off “Enable security defaults” in your Azure portal. Follow the steps below to disable “Enable security defaults” in your Azure portal. Once you’ve disabled “Enable security defaults” in your Azure portal, you can run the Connect-AzAccount command without any problems. Here is the script from the last sub-section’s example. And here are the results of the commands…

How To List All Azure Subscriptions After Conecting With Connect-AzAccount

In the last two examples I showed you how to connect to Azure using the Connect-AzAccount command. After you connect to Azure via PowerShell, you may want to list all available subscriptions in your Azure account. To list all subscriptions in your Azure tenant, run the command below: The command displays all the subscriptions. The subscription IDs are listed in the Id column of the result of the command.

How To Change Azure Subscription After Conecting With Connect-AzAccount

In the last example, I showed you how to list all Azure subscriptions with the Get-AzSubscription command. After listing all available subscriptions, use the Set-AzContext command to change to one of the listed subscriptions. Here is a sample command….

My Final Thoughts About The Connect-AzAccount Cmdlet

The Connect-AzAccount cmdlet is an important cmdlet that all Azure SysAdmins must learn how to use. Based on this, I decided to write this article that explains this all-important Azure PowerShell command. I started the article with an overview of the Connect-AzAccount cmdlet. Then, I explained how to install the Az.Accounts PowerShell Module – required to have the Connect-AzAccount cmdlet on your PC. After that, I discussed the syntaxes and parameters of this cmdlet before I ended the article with a few examples and applications. I hope I made it easy for you to understand this Azure cmdlet. If I absolutely made your day, kindly spare 2 minutes to share your feedback at Itechguides Community Forum. Alternatively, you can keep improving your PowerShell skills by reading more Windows PowerShell Explained guides.

References And Further Reading