# Azure CLI commands cheatsheet

## Introduction

The Azure command-line interface (Azure CLI) is a set of commands used to create and manage Azure resources. The Azure CLI is available across Azure services and is designed to get you working quickly with Azure, with an emphasis on automation.

You can either download Azure CLI on your system ([Microsoft installer](https://aka.ms/installazurecliwindows)). You can get it for Microsoft, Linux, MacOS or even Docker.

Another way to access it is by using Cloud Shell from Azure portal and choosing Bash mode.

![Screenshot of the Azure portal showing the cloud shell icon.](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0c99902-b511-47d8-8110-de8e539742bb_993x473.png align="left")

You can then issue a command as follows

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96289d34-caa4-440d-9ea6-baf58fd7e962_586x257.png align="left")

Azure CLI interactive mode places you in an interactive shell with auto-completion, command descriptions, and examples.

```plaintext
az interactive
```

As a developer, it will help you provision resources by writing a script. Here are some of the commands to get you started.

## Logging in

### Login with web

```plaintext
az login
```

### Login in CLI

```plaintext
az login -u myemail@address.com
```

### List subscriptions

```plaintext
az account list
```

### Set subscription

```plaintext
az account set --subscription "xxx"
OR
az account set -s "xxx"
```

## Listing locations and resources / general

### List all locations

```plaintext
az account list-locations
```

### List all my resources

```plaintext
az resource list
```

### Get what version of the CLI you have

```plaintext
azure --version
```

### Get help

```plaintext
azure help
```

## Creating a basic VM / Resource Group / Storage Account

### Get all available VM sizes

```plaintext
az vm list-sizes --location eastus
```

### List all locations/regions supported in current subscription

```plaintext
az account list-locations -o table
```

### Get all available VM images for Windows and Linux

```plaintext
az vm image list --output table
```

### Create a Linux VM

```plaintext
az vm create --resource-group myResourceGroup --name myVM --image ubuntults
```

### Create a Windows VM

```plaintext
az vm create --resource-group myResourceGroup --name myVM --image win2016datacenter
```

### Create a Resource group

```plaintext
az group create --name myresourcegroup --location eastus
```

### List Resource groups and show in table format

```plaintext
az group list -o table
```

### Create a Storage account.

```plaintext
az storage account create -g myresourcegroup -n mystorageaccount -l eastus --sku Standard_LRS
```

## DELETING A RESOURCE GROUP

### Permanently deletes a resource group

```plaintext
az group delete --name myResourceGroup
```

## Managing VM's

### List your VMs

```plaintext
az vm list
```

### Start a VM

```plaintext
az vm start --resource-group myResourceGroup --name myVM
```

### Stop a VM

```plaintext
az vm stop --resource-group myResourceGroup --name myVM
```

### Deallocate a VM

```plaintext
az vm deallocate --resource-group myResourceGroup --name myVM
```

### Restart a VM

```plaintext
az vm restart --resource-group myResourceGroup --name myVM
```

### Redeploy a VM

```plaintext
az vm redeploy --resource-group myResourceGroup --name myVM
```

### Delete a VM

```plaintext
az vm delete --resource-group myResourceGroup --name myVM
```

### Create image of a VM

```plaintext
az image create --resource-group myResourceGroup --source myVM --name myImage
```

### Create VM from image

```plaintext
az vm create --resource-group myResourceGroup --name myNewVM --image myImage
```

### List VM extensions

```plaintext
az vm extension list --resource-group azure-playground-resources --vm-name azure-playground-vm
```

### Delete VM extensions

```plaintext
az vm extension delete --resource-group azure-playground-resources --vm-name azure-playground-vm --name bootstrapper
```

## Managing Batch Account

### Create a Batch account.

```plaintext
az batch account create -g myresourcegroup -n mybatchaccount -l eastus
```

### Create a Storage account.

```plaintext
az storage account create -g myresourcegroup -n mystorageaccount -l eastus --sku Standard_LRS
```

### Associate Batch with storage account.

```plaintext
az batch account set -g myresourcegroup -n mybatchaccount --storage-account mystorageaccount
```

We can now authenticate directly against the account for further CLI interaction.

```plaintext
az batch account login -g myresourcegroup -n mybatchaccount
```

### Display the details of our created account.

```plaintext
az batch account show -g myresourcegroup -n mybatchaccount
```

### Create a new application.

```plaintext
az batch application create --resource-group myresourcegroup --name mybatchaccount --application-id myapp --display-name "My Application"
```

### Add zip files to application

```plaintext
az batch application package create --resource-group myresourcegroup --name mybatchaccount --application-id myapp --package-file my-application-exe.zip --version 1.0
```

### Assign the application package as the default version.

```plaintext
az batch application set --resource-group myresourcegroup --name mybatchaccount --application-id myapp --default-version 1.0
```

### Retrieve a list of available images and node agent SKUs.

```plaintext
az batch pool node-agent-skus list
```

### Create new Linux pool with VM config

```plaintext
az batch pool create \
    --id mypool-linux \
    --vm-size Standard_A1 \
    --image canonical:ubuntuserver:16.04.0-LTS \
    --node-agent-sku-id “batch.node.ubuntu 16.04”
```

### Now let's resize the pool to start up some VMs.

```plaintext
az batch pool resize --pool-id mypool-linux --target-dedicated 5
```

### We can check the status of the pool to see when it has finished resizing.

```plaintext
az batch pool show --pool-id mypool-linux
```

### List the compute nodes running in a pool.

```plaintext
az batch node list --pool-id mypool-linux
```

If a particular node in the pool is having issues, it can be rebooted or reimaged. A typical node ID will be in the format 'tvm-xxxxxxxxxx\_1-'.

```plaintext
az batch node reboot --pool-id mypool-linux --node-id tvm-123_1-20170316t000000z
```

### Re-allocate work to another node.

```plaintext
az batch node delete \
    --pool-id mypool-linux \
    --node-list tvm-123_1-20170316t000000z tvm-123_2-20170316t000000z \
    --node-deallocation-option requeue
```

### Create a new job to encapsulate the tasks that we want to add.

```plaintext
az batch job create --id myjob --pool-id mypool
```

### Add tasks to the job.

…where is your preferred shell for execution (/bin/sh, /bin/bash, /bin/ksh etc.), and /path/to/script.sh is, of course, the full path of the shell script you’re invoking to get things started.

```plaintext
az batch task create --job-id myjob --task-id task1 --application-package-references myapp#1.0 --command-line "/bin/<shell> -c /path/to/script.sh"
```

### Add many tasks at once

```plaintext
az batch task create --job-id myjob --json-file tasks.json
```

Now that all the tasks are added - we can update the job so that it will automatically be marked as completed once all the tasks are finished.

```plaintext
az batch job set --job-id myjob --on-all-tasks-complete terminateJob
```

### Monitor the status of the job.

```plaintext
az batch job show --job-id myjob
```

### Monitor the status of a task.

```plaintext
az batch task show --job-id myjob --task-id task1
```

### Delete a job

```plaintext
az batch job delete --job-id myjob
```

## Managing Containers

If you HAVE AN SSH run this to create an Azure Container Service Cluster (~10 mins)

```plaintext
az acs create -n acs-cluster -g acsrg1 -d applink789
```

If you DO NOT HAVE AN SSH run this to create an Azure Container Service Cluster (~10 mins)

```plaintext
az acs create -n acs-cluster -g acsrg1 -d applink789 --generate-ssh-keys
```

### List clusters under your whole subscription

```plaintext
az acs list --output table
```

### List clusters in a resource group

```plaintext
az acs list -g acsrg1 --output table
```

### Display details of a container service cluster

```plaintext
az acs show -g acsrg1 -n acs-cluster --output list
```

### Scale using ACS

```plaintext
az acs scale -g acsrg1 -n acs-cluster --new-agent-count 4
```

### Delete a cluster

```plaintext
az acs delete -g acsrg1 -n acs-cluster
```
