Create a virtual machine with the Azure CLI
This script creates an Azure Virtual Machine running Windows Server 2016. After running the script, you can access the virtual machine through a Remote Desktop connection.
To run this sample, install the latest version of the Azure CLI. To start, run
az login to create a connection with Azure.
Samples for the Azure CLI are written for the
bash shell. To run this sample in Windows PowerShell or Command Prompt, you may need to change
elements of the script.
#!/bin/bash # Update for your admin password AdminPassword=ChangeYourAdminPassword1 # Create a resource group. az group create --name myResourceGroup --location westeurope # Create a virtual network. az network vnet create --resource-group myResourceGroup --name myVnet --subnet-name mySubnet # Create a public IP address. az network public-ip create --resource-group myResourceGroup --name myPublicIP # Create a network security group. az network nsg create --resource-group myResourceGroup --name myNetworkSecurityGroup # Create a virtual network card and associate with public IP address and NSG. az network nic create \ --resource-group myResourceGroup \ --name myNic \ --vnet-name myVnet \ --subnet mySubnet \ --network-security-group myNetworkSecurityGroup \ --public-ip-address myPublicIP # Create a virtual machine. az vm create \ --resource-group myResourceGroup \ --name myVM \ --location westeurope \ --nics myNic \ --image win2016datacenter \ --admin-username azureuser \ --admin-password $AdminPassword # Open port 3389 to allow RDP traffic to host. az vm open-port --port 3389 --resource-group myResourceGroup --name myVM
Clean up deployment
Run the following command to remove the resource group, VM, and all related resources.
az group delete --name myResourceGroup --yes
This script uses the following commands to create a resource group, virtual machine, and all related resources. Each command in the table links to command specific documentation.
|az group create||Creates a resource group in which all resources are stored.|
|az network vnet create||Creates an Azure virtual network and subnet.|
|az network public-ip create||Creates a public IP address with a static IP address and an associated DNS name.|
|az network nsg create||Creates a network security group (NSG), which is a security boundary between the internet and the virtual machine.|
|az network nic create||Creates a virtual network card and attaches it to the virtual network, subnet, and NSG.|
|az vm create||Creates the virtual machine and connects it to the network card, virtual network, subnet, and NSG. This command also specifies the virtual machine image to be used, and administrative credentials.|
|az group delete||Deletes a resource group including all nested resources.|
For more information on the Azure CLI, see Azure CLI documentation.
Additional virtual machine CLI script samples can be found in the Azure Windows VM documentation.
Send feedback about: