Use PowerShell to Create Virtual Machine in Azure – Part 3

Summary: Learn how to spin up a virtual machine in Microsoft Azure.

Honorary Scripting Guy, Sean Kearney is here. Ed is still resting nicely, so today, I’ll show you how to spin up a virtual machine in Microsoft Azure!

If you add Get-Help against the New-AzureVM cmdlet, you’ll have an example like this one:

New-AzureVMConfig -Name “MyNewVM” -InstanceSize ExtraSmall -ImageName (Get-AzureVMImage)[4].ImageName |
Add-AzureProvisioningConfig -Windows -Password $adminPassword -AdminUsername PsTestAdmin |
New-AzureVM -ServiceName “MySvc2” -AffinityGroup “Contoso” -WaitForBoot

Let’s break this down. The first part is the virtual machine configuration that is being defined. This requires a name for the virtual machine, the instance size, and a template.

This part is easy now because in the first two days of this series, we obtained the cmdlets to access the template names and the instance names. For more information, see:

Let’s use that to define a configuration for a virtual machine called “HSG-VM1” with an instance size of A0. We’ll use the Windows Server 2012 R2 image.

# Get information needed for Windows Server 2012 R2 image name from Azure

$VMimage=((Get-AzureVMImage | where { $_.Label -like ‘Windows Server 2012 R2*’ } |
Sort-Object PublishedDate)[-1]).ImageName

# Get the information for instance size A0

$Size=(Get-AzureRoleSize | Where { $_.InstanceSize –match ‘A0’ }).InstanceSize

# Virtual machine name


$VMconfig=New-AzureVMConfig –Name $VMName -InstanceSize $Size -ImageName $VMimage

Our next step is to pipe this to Add-AzureProvisioningConfig to define the local Admin account and password:


$AdminPass=Convertto-Securestring –asplaintext ‘NotP@ssw0rd’ -force

$VMProvision = $VMconfig |
Add-AzureProvisioningConfig –windows –password $AdminPass –AdminUserName $AdminName

The final task is to leverage the cmdlet to spin up the virtual machine. But there is one final piece of information missing: the Azure service name. To find out what Azure service names you have defined, you simply need only one cmdlet: Get-AzureService. Unless you like reading massive mounds of information, I suggest piping this to Format-Table in the following manner:

Get-AzureService | Format-Table

With the service name, you can now create the virtual machine in Azure as follows:

$VMResult=$VMProvision | New-AzureVM –Servicename ‘HSG-Service’ –waitforboot

Within moments, we can access the Azure console to see the new virtual machine.

We can, of course, speed up this process by hard coding the image name and instance size. But this presents a more dynamic solution for us.

What will we do next? How about playing with creating a new virtual machine by using Desired State Configuration in Azure? Pop in tomorrow for more details!

I invite you to follow The Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, remember eat your cmdlets every day with a dash of creativity.

Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy 

Comments (1)

  1. debDip says:

    Great topic Sean. But I am facing a problem when I executing New-AzureVMConfig.
    I keep getting this error

    "Error: New-AzureVMConfig : Must specify MediaLocation or set a current storage account using Set-AzureSubscription"

    if I don’t execute this line (which is not mentioned in your blog)
    Set-AzureSubscription -SubscriptionName $subName -CurrentStorageAccount ‘mystorage’

    How to automatically link to a storage or how you’re getting around this issue?

Skip to main content