Create Azure VM using Powershell

Create Azure VM using Powershell is simpler than you think.
In this article I want to show you how to do it in less than 20 lines of code.
Let’s do it 🙂

First of all you must login to Azure Account and select subscription in which you want to create new VM.

Select-AzureRmSubscription -SubscriptionName "Name of your subscription"

In next define 3 variables which will be used for storage account name, resource group name and Location.

$StorageName = "storagename"
$Location = "West Europe"
$ResourceGroupName = "RG-VN-TEST"

Once the subscription is selected and variables are defined we can start with right scripting.
Create new resource group.

New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location

Create storage account on which OS will be installed. Switch which must be defined in proper way is Type (data replication), in our example we will use default one. For other types please check for Microsoft documentation.

$StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageName -Type "Standard_GRS" -Location $Location

Create new subnet, define name and correct subnet mask.

$Subnet = New-AzureRmVirtualNetworkSubnetConfig -Name singleSubnet -AddressPrefix

Create Vritual Network, same as for subnet provide name and mask.

$VNet = New-AzureRmVirtualNetwork -Name TestNet -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix -Subnet $Subnet

Use below command to create new public IP address.
By switch AllocationMethod define if IP should be dynamic or static.

$PublicIP = New-AzureRmPublicIpAddress -Name TestPIP -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic

Once VNET and public IP are created you can proceed with netowrk card creation. Provide correct name.

$NetworkInterface = New-AzureRmNetworkInterface -Name TestNIC -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $PublicIP.Id

Define credentials for local administrator account.

$Credentials = Get-Credential -Message "Type the name and password of the local administrator account."

Define VM config, provide name and VMsize (for size definition go to In our example smallest machine will be used.

$VM = New-AzureRmVMConfig -VMName WindowsVM -VMSize "Standard_A1"

Setup operating system.

$VM = Set-AzureRmVMOperatingSystem -VM $VM -Windows -ComputerName MyWindowsVM -Credential $Credentials -ProvisionVMAgent -EnableAutoUpdate

Select proper source image…

$VM = Set-AzureRmVMSourceImage -VM $VM -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"

… and attach network interface to VM…

$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $nic.Id

define path to blob, where vhd file for our vm will be stored.

$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/WindowsVMosDisk.vhd"
$VM = Set-AzureRmVMOSDisk -VM $vm -Name "windowsvmosdisk" -VhdUri $osDiskUri -CreateOption fromImage

Once all CIs related to VM are created you can proceed with VM creation.

New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VM

That’s all! You can enjoy your new Azure VM. Yes, it is so easy 🙂
Remeber to comment if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.