I grew up with Perl, tcsh, bash, sh, awk, sed, grep, pico, vi, command.com and cmd.exe. Building scripts, batch files and little automation here and there was the norm for me and many others of that era.
I’m especially proud of a set of .BAT files and VBScripts that connected to one of the very first Logitech USB webcams to retrieve a near real-time image of the coffee pot. “Yup, still coffee left I better hurry.” I ran this between 2002 and 2004 until someone dropped the webcam and broke it.
We then got a proper GUI with Windows 95. Operating Systems before this, such as IBM’s OS/2, and Microsoft’s Windows 3.1 and 3.11 did have pretty splendid GUIs but Windows 95 brought the thinking together in a cohesive way.
Suddenly it was all about using a mouse. I started missing out on keyboard shortcuts, quick commands and tricks to become more efficient. I still run ncpa.cpl at least once a day.
Fast forward to late 2006 when PowerShell was first released. We got an inkling of hope amidst working with just a mouse. Since then PowerShell has become a norm for IT Pros and even for some developers. Many things have changed and evolved since then and Microsoft Azure has naturally embraced PowerShell since the beginning.
Until someone came up with the idea of a cross-platform command-line tool that is not PowerShell – the Azure CLI.
This short guide is my attempt to help anyone else moving from PowerShell to Azure CLI or anyone just wanting to do things more efficiently from a command prompt.
What is Azure CLI?
Azure CLI is a cross-platform command line tool, that is used to manage and administrate Microsoft Azure. It doesn’t replace PowerShell but provides an alternative to using managing Azure from the command line. You can still continue using PowerShell, the APIs, and the Azure Portal just like before. Azure CLI provides some tangible benefits over these, especially over PowerShell in that it’s very nimble and can be quickly installed on almost any platform.
Installing Azure CLI
Depending on how you plan on using Azure CLI there are multiple ways of running it. The easiest is to run it locally in Windows, macOS, Linux, or in a Docker container. You can also use Windows Subsystem for Linux (WSL) on Windows 10 also.
Install on Windows
To install on Windows, download and run the installer here.
Install on macOS
To install on macOS, run:
brew update && brew install azure-cli
Install on Linux
To install on Linux, depending on your distro and other settings, follow instructions here.
Install on WSL (on Windows 10)
To install Azure CLI on WSL, you’ll need to install WSL first. To do this, download a Linux distro from the App Store first – in this example I’m downloading Ubuntu 18.04 on a Windows 10:
curl.exe -L -o ubuntu-1804.appx https://aka.ms/wsl-ubuntu-1804
Next, enable the Windows Subsystem for Linux using PowerShell:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Note: You’ll need to reboot after this command has completed.
Finally, execute ubuntu-1804.appx. Once this is finished run the distro from the Start menu. This will initiate the final configuration for WSL.
When WSL is running you can continue with Linux installation based on your distro.
Install on Docker
Once you have Docker running and configured, simply run:
docker run -it microsoft/azure-cli
This pulls down the Docker container for Azure CLI and runs it interactively using a pseudo-TTY to initiate a Bash shell.
Azure CLI in Azure Cloud Shell
Another alternative is to use Azure CLI in Azure Cloud Shell, which runs within the Azure Portal. Simply open https://portal.azure.com and click the Cloud Shell icon in the top toolbar.
Azure CLI even works in a mobile phone (via the Azure App or Azure Cloud Shell)
Azure CLI in Visual Studio Code
You can also run Azure CLI in Visual Studio Code. It’s available as a free extension.
Visual Studio Code gives Intellisense support and awesome help when writing larger Azure CLI scripts. All you have to do is to name your script files with the .azcli extension for VSCode to pick them up.
Authenticating to Azure
Time to start using Azure CLI! To login, you can simply type
Note: If you’re using Azure Cloud Shell you do not need to login separately.
If you have multiple Azure subscriptions with different (or overlapping) login accounts, use a specific device code:
az login --use-device-code
This allows you to manually perform the authentication in a browser session of your choice. Once completed you can close the browser tab and Azure CLI completes the login.
There are plenty of other options for logging in, especially if you choose to use a Service Principal or prompt for a username and password. To see these options, use:
az login --help
You can verify you’ve logged in successfully by listing your Azure subscriptions:
az account list --all
To set the default subscription for Azure CLI, use:
az account set --subscription "Azure Production"
Using Azure CLI interactively
Finally, regardless of which OS and platform you’re using to run Azure CLI you can install an extension to run Azure CLI interactively. To do this, run:
Formatting output data
To format data your commands in Azure CLI produce you can choose between the following formats:
- json – regular JSON output
- jsonc – JSON.. but with colors!
- yaml – output in “YAML Ain’t Markup Language”
- table – ASCII table because 1985
- tsv – Tab-separated values
To try any of these out, consider the following command:
az cloud list
This lists all the available Clouds, such as GCC, Azure German Cloud and similar that you can access. To try out the different outputs, simply append –output <type>:
az cloud list --output table
You can also configure one of these output types to be the default. Simply run:
And select the preferred options.
What can you do with Azure CLI?
So, now what? You’ve successfully logged in to Azure with Azure CLI and it just works. You can, in practice, do anything with Azure CLI you would typically use Azure Portal for.
Most Azure CLI commands follow the same notion of az <command group> <parameters>. You can substitute command group with dozens of options, such as group (for Resource Group), vm (for Virtual Machine), webapp (for Web App) and similar.
To view all available command groups simply type
Managing Resource Groups
To list all Resource Groups, use:
az group list
To create a new Resource Group, type:
az group create --name <name> --location <azurelocation>
To destroy a Resource Group, and when you really mean it, use:
az group delete --resource-group foo --yes --no-wait
–yes confirms the deletion, and –no-wait to allow the script to continue without waiting for the command to finish.
Using variables with Azure CLI
When your scripts and provisioning logic start to grow it makes sense to use variables. Depending on your platform you set variables in the usual way. In Windows command prompt, this would be with:
In PowerShell, it’s almost the same:
To use a variable in your Azure CLI script simply reference it according to your platforms notation. In PowerShell this would be:
$LOCATION=westeurope az group create --name foo --location $LOCATION
Sometimes you might not be sure which command to use for the task at hand. To search for commands use:
az find --search-query <keyword>
Sometimes you’ll want to shorten your commands. Azure CLI supports plenty of abbreviations that you typically find out when using different commands for the first time. As an example, this command:
az group show --resource-group aci-demo
Could be written in shorter form as:
az group show -g aci-demo
I typically find I get by with just using the az command and only occasionally looking up for additional help. For this I use the official Azure CLI reference.
Thanks for reading – and happy scripting with Azure CLI!
I work with Azure and frequently write about my experiences. I’m a Microsoft Most Valuable Professional, ex-MSFT. Based in Helsinki, Finland.