Common PowerShell commands for creating and managing Azure Virtual Machines

This article covers some of the 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 Get-Help command.

See How to install and configure Azure PowerShell for information about installing the latest version of Azure PowerShell, selecting your subscription, and signing in to your account.

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

  • $location - The location of the virtual machine. You can use Get-AzureRmLocation 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

Task Command
Create a VM configuration $vm = New-AzureRmVMConfig -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-AzureRmVMOperatingSystem -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-AzureRmVMConfig.
Add a network interface $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

A VM must have a network interface to communicate in a virtual network. You can also use Get-AzureRmNetworkInterface to retrieve an existing network interface object.
Specify a platform image $vm = Set-AzureRmVMSourceImage -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-AzureRmVMConfig. The object returned from this command is only used when you set the OS disk to use a platform image.
Set OS disk to use a platform image $vm = Set-AzureRmVMOSDisk -VM $vm -Name "myOSDisk" -VhdUri "http://mystore1.blob.core.windows.net/vhds/myOSDisk.vhd" -CreateOption FromImage

The name of the operating system disk and its location in storage is added to the configuration object that you previously created.
Set OS disk to use a generalized image $vm = Set-AzureRmVMOSDisk -VM $vm -Name "myOSDisk" -SourceImageUri "https://mystore1.blob.core.windows.net/system/Microsoft.Compute/Images/myimages/myprefix-osDisk.{guid}.vhd" -VhdUri "https://mystore1.blob.core.windows.net/vhds/disk_name.vhd" -CreateOption FromImage -Windows

The name of the operating system disk, the location of the source image, and the disk's location in storage is added to the configuration object.
Set OS disk to use a specialized image $vm = Set-AzureRmVMOSDisk -VM $vm -Name "myOSDisk" -VhdUri "http://mystore1.blob.core.windows.net/vhds/" -CreateOption Attach -Windows
Create a VM New-AzureRmVM -ResourceGroupName $myResourceGroup -Location $location -VM $vm

All resources are created in a resource group. Before you run this command, run New-AzureRmVMConfig, Set-AzureRmVMOperatingSystem, Set-AzureRmVMSourceImage, Add-AzureRmVMNetworkInterface, and Set-AzureRmVMOSDisk.

Get information about VMs

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

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

Manage VMs

Task Command
Start a VM Start-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Stop a VM Stop-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Restart a running VM Restart-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Delete a VM Remove-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Generalize a VM Set-AzureRmVm -ResourceGroupName $myResourceGroup -Name $myVM -Generalized

Run this command before you run Save-AzureRmVMImage.
Capture a VM Save-AzureRmVMImage -ResourceGroupName $myResourceGroup -VMName $myVM -DestinationContainerName "myImageContainer" -VHDNamePrefix "myImagePrefix" -Path "C:\filepath\filename.json"

A virtual machine must be prepared, shut down and generalized to be used to create an image. Before you run this command, run Set-AzureRmVm.
Update a VM Update-AzureRmVM -ResourceGroupName $myResourceGroup -VM $vm

Get the current VM configuration using Get-AzureRmVM, change configuration settings on the VM object, and then run this command.
Add a data disk to a VM Add-AzureRmVMDataDisk -VM $vm -Name "myDataDisk" -VhdUri "https://mystore1.blob.core.windows.net/vhds/myDataDisk.vhd" -LUN # -Caching ReadWrite -DiskSizeinGB # -CreateOption Empty

Use Get-AzureRmVM to get the VM object. Specify the LUN number and the size of the disk. Run Update-AzureRmVM to apply the configuration changes to the VM. The disk that you add is not initialized.
Remove a data disk from a VM Remove-AzureRmVMDataDisk -VM $vm -Name "myDataDisk"

Use Get-AzureRmVM to get the VM object. Run Update-AzureRmVM to apply the configuration changes to the VM.
Add an extension to a VM Set-AzureRmVMExtension -ResourceGroupName $myResourceGroup -Location $location -VMName $myVM -Name "extensionName" -Publisher "publisherName" -Type "extensionType" -TypeHandlerVersion "#.#" -Settings $Settings -ProtectedSettings $ProtectedSettings

Run this command with the appropriate configuration information for the extension that you want to install.
Remove a VM extension Remove-AzureRmVMExtension -ResourceGroupName $myResourceGroup -Name "extensionName" -VMName $myVM

Next steps