Common PowerShell commands for creating and managing Azure Virtual Machines

Applies to: ✔️ Linux VMs ✔️ Windows VMs ✔️ Flexible scale sets

This article covers some of the basic Azure PowerShell commands that you can use to create and manage virtual machines in your Azure subscription. For more detailed help with specific command-line switches and options, you can use the Get-Help command.

These variables might be useful if running more than one of the commands in this article:

  • $location - The location of the virtual machine. You can use Get-AzLocation to find a geographical region that works for you.
  • $myResourceGroup - The name of the resource group that contains the virtual machine.
  • $myVM - The name of the virtual machine.

Create a VM - simplified

Task Command
Create a simple VM New-AzVM -Name $myVM



New-AzVM has a set of simplified parameters, where all that is required is a single name. The value for -Name will be used as the name for all of the resources required for creating a new VM. You can specify more, but this is all that is required.
Create a VM from a custom image New-AzVm -ResourceGroupName $myResourceGroup -Name $myVM ImageName "myImage" -Location $location



You need to have already created your own managed image. You can use an image to make multiple, identical VMs.

Create a VM - advanced

Task Command
Create a VM configuration $vm = New-AzVMConfig -VMName $myVM -VMSize "Standard_D1_v1"



The VM configuration is used to define or update settings for the VM. The configuration is initialized with the name of the VM and its size.
Add configuration settings $vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName $myVM -Credential $cred -ProvisionVMAgent -EnableAutoUpdate



Operating system settings including credentials are added to the configuration object that you previously created using New-AzVMConfig.
Add a network interface $vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id



A VM must have a network interface to communicate in a virtual network. You can also use Get-AzNetworkInterface to retrieve an existing network interface object.
Specify a platform image $vm = Set-AzVMSourceImage -VM $vm -PublisherName "publisher_name" -Offer "publisher_offer" -Skus "product_sku" -Version "latest"



Image information is added to the configuration object that you previously created using New-AzVMConfig. The object returned from this command is only used when you set the OS disk to use a platform image.
Create a VM New-AzVM -ResourceGroupName $myResourceGroup -Location $location -VM $vm



All resources are created in a resource group. Before you run this command, run New-AzVMConfig, Set-AzVMOperatingSystem, Set-AzVMSourceImage, Add-AzVMNetworkInterface, and Set-AzVMOSDisk.
Update a VM Update-AzVM -ResourceGroupName $myResourceGroup -VM $vm



Get the current VM configuration using Get-AzVM, change configuration settings on the VM object, and then run this command.

Get information about your VMs

Task Command
List VMs in a subscription Get-AzVM
List VMs in a resource group Get-AzVM -ResourceGroupName $myResourceGroup



To get a list of resource groups in your subscription, use Get-AzResourceGroup.
Get information about a VM Get-AzVM -ResourceGroupName $myResourceGroup -Name $myVM

Manage your VMs

Task Command
Start a VM Start-AzVM -ResourceGroupName $myResourceGroup -Name $myVM
Stop a VM Stop-AzVM -ResourceGroupName $myResourceGroup -Name $myVM
Restart a running VM Restart-AzVM -ResourceGroupName $myResourceGroup -Name $myVM
Delete a VM Remove-AzVM -ResourceGroupName $myResourceGroup -Name $myVM

Next steps