Choosing between PowerShell and the Azure CLI – Scripted Deployments and DevOps Automation

When we start to consider automating the deployment and management of resources in Azure, the first option would be using the command line, and here we then have two further options: PowerShell and the Azure CLI.

PowerShell and the CLI are very similar: they both run from a command prompt, they both allow access to the Azure platform, and the commands are also very similar.

Traditionally, PowerShell was only available on Windows computers; however, in recent years, PowerShell has been made available on macOS and Linux platforms. PowerShell has a much broader use than Azure and can be used on Windows platforms to manage and script many processes and Windows management functions.

PowerShell is also extensible: you can build functions, classes, scripts, and modules in a similar fashion to development languages. It is a separate module, called the Az PowerShell module, that enablesAzure functionality.

Information Note

The current Az PowerShell module is a replacement for an older AzureRM module that was originally built to provide the same level of functionality. AzureRM has since been deprecated and you should remove AzureRM before you install Az PowerShell if you had previously installed AzureRM.

The Azure CLI is a standalone set of commands, which means that you do not need PowerShell. The Azure CLI is available for Windows, macOS, and Linux environments.

Because both options are available cross-platform, the choice of one over the other is largely a personal choice; however, the Azure CLI is a single install on macOS and Linux, whereas Az PowerShell requires PowerShell to be installed first.

Both options can also be used in Azure Cloud Shell, which is an interactive browser-based tool available in the Azure portal.

When using PowerShell or the Azure CLI through Cloud Shell, you are already authenticated; however, when using the tools from your own computer, you must first authenticate. Remember from the previous section that the Azure REST APIs required a client access token, and because command-line tools essentially wrap calls to those APIs, you must log in to obtain the token.

We shall see next how similar the two options are by showing some example commands.

Signing in to Azure

The first step in using either tool is to authenticate against Azure:

  • PowerShell:

connect-AzAccount

  • Azure CLI:

az login

Depending on your OS and command line choice, the sign in process is slightly different; however, once authenticated, a list of subscriptions you have access to will be returned.

Selecting a subscription

If you have access to multiple subscriptions, you will need to select which one to use:

  • PowerShell:

Select-AzureSubscription -SubscriptionName “<subscription name>”

  • Azure CLI:

az account set –subscription “<subscription name>”

Once set, we can now perform actions.

Listing resource groups

The following examples simply list all resource groups in your selected subscription:

  • PowerShell:

get-AzResourceGroup

  • Azure CLI:

az group list

Both commands will return a list of resource groups.

Information Note

A full list of Azure CLI commands can be found here: https://docs.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest.

A full list of Az PowerShell commands can be found here: https://docs.microsoft.com/en-us/powershell/azure/?view=azps-5.8.0.

Through PowerShell or the Azure CLI, you can perform any task, from obtaining information to creating, managing, and deleting resources.

One of the great benefits of either option is that you can perform actions on multiple resources by building logic-based routines. For example, if you wanted to build 100 VMs, you could write a script that would loop 100 times, automatically creating each VM.

The main drawback to scripting is that scripts can become quite complex, especially when deploying resources built from many different components; for example, VMs are actually built with additional components such as disks and network interface cards. Both PowerShell and the Azure CLI can be used with another core feature of Azure: the ability to create JSON-based templates for resources, as we will examine next.

Leave a Reply

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