In this article, you'll learn how to create a Windows Server 2012 R2 virtual machine (VM) in Microsoft Azure.

Timothy Warner

Timothy Warner is a Microsoft Cloud and Datacenter Management Most Valuable Professional (MVP) who is based in Nashville, TN. Check out his Azure and Windows Server video training at Pluralsight, and feel free to reach out to Tim via Twitter.

Latest posts by Timothy Warner (see all)

To prepare, you'll need to have an Azure subscription. (You can sign up for a free trial.)

We don't need to log into the Azure Management Portal to create and manage VMs. Instead, we'll use the Windows PowerShell Integrated Scripting Environment (ISE) and the Azure PowerShell module.

Once you've installed the Azure module and loaded up an administrative ISE session, you're ready to follow the instructions below.

Step #1: Connect to your Azure subscription ^

The first thing we need to do is sign in to Azure:

You’ll have to provide your Azure credentials.

Next, you have to create a storage account because the VM image will be stored in Azure Storage:

Note that the storage account name must be unique in Azure. If you receive an error message that the name is already taken, you have to repeat the command with another storage account name. An overview of the available locations can be found here.

Next, I use Set-AzureSubscription to set the new storage account as the current storage account for my Azure subscription. You can get the name of your subscription with Get-AzureSubscription; in the example below, mine is '150dollar':

Step #2: Find your desired OS image ^

We now programmatically access the Azure operating system (OS) image gallery to get the "latest and greatest" Windows Server 2012 R2 image.

The following Get-AzureVMImage pipeline retrieves the most recent image name, however honkin' long it is. Once you have it, select it and copy it to your clipboard, because we'll need it in the next step.

I want you to improve your PowerShell skills; so many Windows systems administrators I know can copy and paste PowerShell code all day long, but few understand what the code does. Let me break down the previous pipeline, piece by piece:

  • First, we retrieve all Azure VM images from the Microsoft gallery.
  • Second, we extract three key properties from the original dataset (which is very large).
  • Third, we filter to retrieve only images that start with 'Windows Server 2012 R2'.
  • Fourth, we perform a descending sort on publication date. (Remember that our goal is to get the most recent image.)
  • Fifth, we run Select-Object to reduce our results only to the most recent OS image.
  • Sixth and finally, we put the view in list format for easier browsing.

Step #3: Create your VM configuration ^

We need to create a VM configuration before we use New-AzureVM to create the virtual machine.

I'm a fan of storing settings in variables and then passing the variables into cmd-lets as parameter values.

Of course, I'm not going to leave you hanging in the breeze, throwing code at you without explanation. Let me briefly explain those three variables:

  • $image: This variable stores the OS image name we found in a previous step. Note that the image name changes from time to time.
  • $vmname: This is the DNS hostname of our new VM.
  • $vmsize: This is the VM instance size. Be careful that you're choosing a size that is (a) correct for the VM you're deploying and (b) consistent with your spending budget.

Now we'll use New-AzureVMConfig to create a VM configuration and store the object in the $vm1 variable:

We can use Get-Credential and Add-AzureProvisioningConfig to safely capture a username and password and attach them to our new VM's administrative account. They will be the credentials that you need to sign in to the Windows server.

Notice that we pipe our $vm1 object into Add-AzureProvisioningConfig; that's a convenient tip for adding functionality to an object that's already resident in your PowerShell session's memory.

Step #4: Create the VM ^

First, we have to create a new cloud service, which provides a container for our server and assigns a unique public DNS name and IP to the server. The public DNS name consists of the service name plus In our example, the DNS name would be ''. Thus, the service name has to be unique in Azure.

We then use the service name to create the VM with the New-AzureVM cmdlet:

We have to ensure here that cloud service is in the same Azure region as our storage account. I like to use the –WaitForBoot switch parameter, because it will fail VM creation if the VM doesn't reach ReadyRole (provisioned and ready for launch) status.

We can then invoke Get-AzureVM to verify the state of our new VM:

It's possible to create the Remote Desktop Connection (.RDP) connection files programmatically. However, at this point, it's probably easier for you to log into the Azure Management Portal, select your virtual machine, and click Connect, as shown in the following screenshot:

Creating our connection .RDP file

Creating our connection .RDP file

As you can see in the following screen capture, my DC1 virtual machine is up and running!

Our Azure cloud-based VM

Our Azure cloud-based VM

A final note: If you're just testing, please make sure not only to stop but to deallocate your virtual machine, to ensure that you don't get charged by Microsoft for reserving computational power for your "simply powered off" VM. Sneaky, eh?

You shut down and deallocate your VM by running Stop-AzureVM with the –Force switch.

In the next example, I'm using Where-Object to stop and deallocate all my running VMs:

Your VM status will change to StoppedDeallocated once you've successfully run the previous command.

In the next articles of this series, we will show you how you can create an Azure VM via the Azure Resource Manager (ARM).

Join the 4sysops PowerShell group!

1 Comment
  1. Aleksandrs 3 years ago

    Hi, it was very useful article, it helped me. But you can add what StorageAccountNam must be with small letters. Cause I didn't know about it, and I needed more time to find why I had an error.

    Thank you a lot.


Leave a reply

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


© 4sysops 2006 - 2019


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account