denisismagilov - Fotolia

Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Evaluate Azure CLI vs. PowerShell for resource management

Compare two popular resource management tools for Azure -- Azure CLI and PowerShell -- to determine if one, or a combination of both, is right for your cloud environment.

When it comes to resource management in Microsoft Azure, there are two primary tools IT teams can use: Azure CLI and PowerShell. One question that comes up frequently is when to use one or the other.

Sometimes the choice between the two tools depends on what is familiar. For those coming from an on-premises environment, PowerShell is the more natural choice since it's been a part of Microsoft Windows for more than a decade, but the Azure command-line interface (Azure CLI) has its merits, too.

The differences between the two are often subtle, and your selection will likely come down to personal preference. Thankfully, though, the process of choosing is made easier by the fact that the Azure CLI and PowerShell call the same exact APIs. Let's take a closer look at Azure CLI and PowerShell, as well as the possibility of combining them.

What are Azure CLI and PowerShell?

Before jumping into the code, let's talk briefly about PowerShell and the Azure CLI and why they are so popular.

Azure CLI

The Azure CLI is a set of commands developers use to create, manage and interact with all Azure resources from a programmatic perspective. The Azure CLI is built in Python. You can use the Azure CLI from both the terminal on your computer and the Azure Cloud Shell, which is a browser-accessible shell.

Additionally, Azure CLI is cross-platform, which means IT teams can use it on any OS, including:

  • Linux distributions
  • MacOS
  • Windows


While it was originally only for Windows, PowerShell was re-created as an open source scripting and programming language with an interactive command-line shell. Microsoft built it to serve as an automation tool for system administrators. Unlike most command-line shells, PowerShell was built on the .NET framework and works with objects.

Similar to the Azure CLI, IT teams can run PowerShell on any OS. Developers can also use it to interact with any platform or application that has a public-facing API. PowerShell is still one of the top languages used, alongside Python and JavaScript, for scripting and automating in Azure.

Create a resource in Azure

From a comparison perspective, the best way to evaluate Azure CLI vs. PowerShell in action is to create a resource. Here, developers can see the differences in length of code, ease of use and speed.

Let's create an Azure Resource Group to see the comparisons. An Azure resource group is a collection of related Azure cloud components such as virtual machines, storage accounts, web applications, databases and virtual networks. You can select which resources are grouped together based on your needs.

To write a proper, reusable PowerShell script, you need to have various factors, including functions and parameters. The code below is ready to go, but it is bulky if you're trying to do something quick, like create a resource group.

function New-ResourceGroup {

     param (

     $params = @{
          'Name' = $rgName
          'Location' = $location

     if ($pscmdlet.ShouldProcess('location')) {
     New-AzResourceGroup @params

Now, look at the same task using Azure CLI in the code below.

az group create -l eastus2 -n resource_group_name

As you can see in this situation, the Azure CLI code is significantly shorter compared to PowerShell. To quickly create resources, Azure CLI's codebase is much smaller and a bit easier.

Combine PowerShell and Azure CLI

One benefit of these two tools is developers can combine them for more functionality. Azure CLI is a good choice, but there's no solid way to make the code reusable by itself. For example, if someone else wants to use the code and change the name of the resource group that's being created, developers have to create variables in a terminal session and then the code can run. When you add PowerShell into the mix, you get additional features such as error handling and reusability.

As you can see in the example below, the code uses a PowerShell function and adds in the Azure CLI. This ensures that the Azure CLI has reusable code and error handling added in, to make it a true script.

function New-ResourceGroup {

     param (

     try {
          az group create -l $location `
                          -n $rgName

     catch {

Combining both PowerShell and Azure CLI is a great approach if you cannot decide which one to use.

Use Azure CLI with other programming languages

Let's say you're a Python developer and you want to try PowerShell, but the rest of the stack you work on is Python, so you decide to stay with Python. However, IT teams in this scenario can still use the Azure CLI with other programming languages.

Below is an example of using the Azure CLI with Python. If you're a Python developer, as you can see, you're writing Python code the same way you would always write it. There's no difference in syntax.

The azure.cli.core library enables you to use an invoke function, which you can use to type out an Azure CLI command. This scenario is listing VMs in a specific resource group.

from azure.cli.core import get_default_cli as azcli
import logging
import sys

def list_vms(resource_group):
     azcli().invoke(['vm', 'list', '-g', resource_group])

resource_group = sys.argv[1]

if __name__ == '__main__':
     print('Running as main program...')

Dig Deeper on Managing cloud infrastructure