How to create an Azure Virtual Machine with PowerShell.

So you want to create a Virtual Machine on Azure, this can be done in the portal. But really we want this scriptable, so that we can run it at a later date if needed and also so we can leverage this work and use it to create other virtual machines at a later date.

First, fire up a PowerShell console and login to your subscription:

Login-AzureRmAccount
1. Create a Resource Group

We need to create a Resource Group, this is a logical place to group all the elements that we’re going to create.

$resourceGroup = "test-infra"
$location = "North Europe"

New-AzureRmResourceGroup -Name $resourceGroup -Location $location
2. Create storage account

Now we need a storage account where our virtual machine data will be stored

The storage account name has to be unique globally, so make sure you rename here

$storageAccountName = "teststorage1x2" # Add random number/characters here
New-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroup -Type Standard_LRS -Location $location
3. Create Virtual Network

We need a Virtual Network where our Virtual Machine will live.

$vnetName = "test-net"
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name frontendSubnet -AddressPrefix 10.0.1.0/24
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet
4. Setup IP Address and Network Interface

Our Virtual Machine needs a Network Interface and IP Address associated with it. We’re going to create a Dynamic IP Address for now, but you can have a static IP Address too.

$nicName ="testvm-nic"
$pip = New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
5. Start Virtual Machine config setup

Now we can begin with the actual configuation of the Virtual Machine. We’ll give it a name of testvm1, and have a size of Basic A1.

$vmName = "testvm1"
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize "Basic_A1"
6. Set Virtual Machine Credentials

We create a $cred object to be used later, you can either have the script prompt you for your credentials, or have it hard coded in your script. I would suggest prompt you, as you don’t want to store your password in plain text anywhere.

$cred=Get-Credential -Message "Admin credentials"  # Either this for the prompt or
#$username = "YOURUSERNAME"
#$password = ConvertTo-SecureString "YOUR_PASSWORD" -AsPlainText -Force
#$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
7. Select Operating System for Virtual Machine

We want a Windows Virtual Machine, and pass in the $cred object that we just setup. We can choose which Windows image we want from the Azure gallery, here I have selected the Windows 2012 R2 Datacenter image.

$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter" -Version "latest"
8. Add Network Interface to Virtual Machine
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
9. Create Disk for Virtual Machine

Our Virtual Machine needs somewhere to save itself obviously. So we create a new VHD in the Storage Account we setup earlier.

$diskName="os-disk"
$storageAcc=Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$osDiskUri= $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $diskName + ".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
10. Create the Virtual Machine

Everything should now be configured, it’s time to create the actual Virtual Machine.

New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm

Wrap up

So there we go, we now have a nice new Virtual Machine setup. And we can create a new one really simply by changing a couple of parameters.

You can find the full script example on GitHub here.

Any Azure topics you would like to see? Please reach out!