Store Azure CLI Output as Variable

This Microsoft Azure CLI blog post will show how to store command output as variables.

Azure CLI is a command-line tool that enables developers and system administrators to manage resources in the Azure cloud using simple commands.

It is an open-source, cross-platform command-line interface that is designed to be easy to use and to support a wide range of Azure services. With Azure CLI, users can deploy, configure, and manage Azure services from the command line, either locally or remotely.

Using Variables

Azure CLI uses the LMESPath query language (JSON query language) to retrieve specific information about Azure resources. The following Azure CLI code will show you how to retrieve a VM’s name, store it in a variable, and display it.


To run the following code, set your resource group and VM name. Also, note that variables are stored as $varname$=(azure cli command)

# Get the VM name
$vmName=$(az vm show --resource-group MYRG --name VMNAME --query 'name' -o tsv)

# Print the VM name
echo $vmName

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.