Use PowerShell to Create Virtual Machine in Azure – Part 1

Doctor Scripto

Summary: Learn how to use the Azure PowerShell cmdlets to identify built-in virtual machine templates.

Honorary Scripting Guy, Sean Kearney, is here today to help out and give Ed a break (enjoy some tea and rest your fingers, my friend).

In November, I touched on how to use Windows PowerShell for some overall setup and management of the Azure infrastructure-as-a-service (IaaS). (If you'd like to read more, see this series of Hey, Scripting Guy! Blog posts: Manage Azure IaaS with Windows PowerShell.)

Today, I'm going to specifically focus on creating those virtual machines and all you'll need to know to get yourself really going.

If you've created an virtual machine in Azure, you know it is a pretty simple process in which you choose some pretty basic parameters:

  • Operating system version
  • Virtual machine name
  • Type of tier
  • Size of the virtual machine (A0, A1, D0, D1…)
  • A default user name and password
  • A cloud service
  • Region to store the virtual machine and cloud service
  • A storage account

This is actually everything you select within the Microsoft Azure console. Of course, the cool part is all of this can be done with Windows PowerShell.

The first part is to get yourself authenticated in Azure. This is done with one cmdlet:


This will produce an interactive prompt that asks you for the credentials for your Azure environment. If you really want to automate this you can.

This cmdlet has a –Credential parameter that can accept a object. But to automate this object, it MUST be a user ID in Azure that is an organization ID (such as one that ends in ""). It cannot be your "" account.

When you are connected to Azure, creating the virtual machine from a built-in template is not too tricky. A basic virtual machine uses three cmdlets: New-AzureVMConfig, Add-AzureProvisioningConfig, and New-AzureVM.

For any of these cmdlets to be useful, you need to be able to find the names of the images that Azure has for you. This list is forever growing! You can access this list with the following cmdlet:


But rather than reading this list after chugging down a full pot of coffee, you really only need three objects from it. They are Label, ImageName, and PublishedDate.

With this information, you can easily search through the list of images based on the label, which contains the description of the image and a quick summary of the date it was published. For example, if you want to access all the Windows Server 2012 R2 images, run the following command.

Get-AzureVMImage | Where { $_.Label –like 'Windows Server 2012R2' }

This will produce a long list of output. To make it more readable, you'll want to select the objects by specifically using, of course, Select-Object:

Get-AzureVMImage | Where { $_.Label –like 'Windows Server 2012R2' } | Select-Object Label,PublishedDate,ImageName

Image of command output

You'll notice that in many cases, Azure contains multiple revisions of the images. In this way, you can have a selection of various versions for testing.

If you'd always like the most current version for your image, run this through Sort-Object and capture it in the following manner:

$VMimage=(Get-AzureVMImage | Where { $_.Label –like 'Windows Server 2012R2' } | Sort-Object PublishedDate)[-1]

If you play more thoroughly with Get-VMImage, you'll find that there is even a property to help you know the recommended virtual machine size for deployment! Believe it or not, it's called RecommendedVMSize. I thought that was pretty cool!

Stop by tomorrow and we'll continue our work creating a virtual machine in Azure!

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


Discussion is closed.

Feedback usabilla icon