Create an Azure Virtual Machine with the Azure CLI.

To round off things nicely, I thought I would follow on from two previous posts about creating Azure Virtual Machines. First we went through how to create an Azure Virtual Machine using PowerShell, then we went through with ARM templates. Now we’re going to use the Azure CLI for the third try.

Create an Azure Virtual Machine with PowerShell

Create an Azure Virtual Machine with an ARM template

Create an Azure Virtual Machine with Terraform

The plan

  1. Create a new Resource Group
  2. Create Virtual Network and Subnet
  3. Create Virtual Machine
  4. Open required ports
  5. Connect to the Virtual Machine

Prerequisites

Before you begin, ensure you have:

  • The latest version of the Azure CLI installed
  • An active Azure subscription
  • Basic understanding of Azure networking concepts

If you haven’t installed Azure CLI yet, follow the instructions on the official documentation.

1. Create a new Resource Group

First, set the variables we’ll use throughout this exercise:

1
2
3
4
5
# Set variables
ResourceGroupName="rg-windows-vm1"
Location="northeurope"
VmName="myWindowsVM"
AdminUsername="azureuser"

For the admin password, it’s best to create it securely:

1
2
# Create a secure password (don't expose in scripts)
AdminPassword="YourSecurePassword123!"  # Replace with your secure password

Now create the resource group:

1
2
# Create resource group
az group create --name $ResourceGroupName --location $Location

This command creates a new resource group in the North Europe region where we’ll deploy all our resources.

2. Create Virtual Network and Subnet

1
2
3
4
5
6
7
# Create VNet and subnet
az network vnet create \
  --resource-group $ResourceGroupName \
  --name myVnet \
  --address-prefix 10.0.0.0/16 \
  --subnet-name mySubnet \
  --subnet-prefix 10.0.1.0/24

This creates a virtual network with address space 10.0.0.0/16 and a subnet within it with address space 10.0.1.0/24. The virtual network will provide network isolation for your VM.

3. Create a Network Security Group

Let’s create a network security group with rules to allow RDP (port 3389) traffic:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Create NSG and allow RDP
az network nsg create \
  --resource-group $ResourceGroupName \
  --name myNetworkSecurityGroup

az network nsg rule create \
  --resource-group $ResourceGroupName \
  --nsg-name myNetworkSecurityGroup \
  --name allow-rdp \
  --protocol tcp \
  --priority 1000 \
  --destination-port-range 3389 \
  --access allow

4. Create a Public IP Address

1
2
3
4
5
# Create public IP
az network public-ip create \
  --resource-group $ResourceGroupName \
  --name myPublicIP \
  --allocation-method Dynamic

5. Create a Network Interface

1
2
3
4
5
6
7
8
# Create network interface
az network nic create \
  --resource-group $ResourceGroupName \
  --name myNIC \
  --vnet-name myVnet \
  --subnet mySubnet \
  --public-ip-address myPublicIP \
  --network-security-group myNetworkSecurityGroup

This creates a network interface with the public IP we created and associates it with our subnet and security group.

6. Create the Virtual Machine

1
2
3
4
5
6
7
8
9
# Create VM
az vm create \
  --resource-group $ResourceGroupName \
  --name $VmName \
  --nics myNIC \
  --image MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest \
  --admin-username $AdminUsername \
  --admin-password $AdminPassword \
  --size Standard_D2s_v3

This command creates a Windows Server 2019 VM with the specified size and credentials.

7. Connect to Your Virtual Machine

After your VM is created, you can get its public IP address:

1
2
3
4
5
6
7
# Get public IP
az vm show \
  --resource-group $ResourceGroupName \
  --name $VmName \
  --show-details \
  --query publicIps \
  --output tsv

Use the returned IP address to connect to your VM via RDP.

8. Clean Up Resources (Optional)

When you’re finished testing, you can remove all the resources created in this exercise to avoid incurring additional costs:

1
2
3
4
5
# Delete resource group and all contained resources
az group delete \
  --name $ResourceGroupName \
  --yes \
  --no-wait

Conclusion

In this article, we’ve seen how to use the Azure CLI to create a Windows virtual machine in Azure. The Azure CLI provides a straightforward, script-friendly way to manage Azure resources from the command line.

Unlike the PowerShell and ARM template methods we explored in previous articles, the Azure CLI is cross-platform, making it a great choice if you work across different operating systems. The commands are also generally shorter and more intuitive than their PowerShell counterparts.

Whether you prefer PowerShell, ARM templates, Terraform, or the Azure CLI, Azure provides multiple options to help you automate your infrastructure deployment according to your preferences and requirements.