Managing Azure IaaS with Windows PowerShell: Part 5

Doctor Scripto

Summary: Use Windows PowerShell to manage virtual machines in Azure.

Honorary Scripting Guy, Sean Kearney, is here flying through the digital stratosphere on our final day with Windows PowerShell and Azure!

We started by creating a virtual network for our Azure workstations and escalated to spinning up some virtual machines. To read more, see the previous topics in this series:

Pretty cool! But we're dealing with the fun stuff today…

Normally in Azure, if you were to initiate a shutdown in Windows or Linux, the operating system shuts down, but the Azure resources remain live in the service. The answer to this issue is to access your management portal and shut down the virtual machine from the Portal in the following manner.

First select the virtual machine (in this case, the one we previous created called 'brandnew1'):

Image of menu

Then choose the Shut Down option at the bottom of the management portal:

Image of menu

This process is pretty simple and it doesn't take more than a minute or so. But, of course, it would be so much nicer to have the ability to shut down or start environments by using a script. This allows for better cost control in Azure and less time with you at the mouse going clickity click click click.

In Hyper-V, we would normally identify the virtual machines with Get-VM and then parse the output to the pipeline to Stop-VM.

Azure is not too different—other than the names of the cmdlets and the visual output.

With Azure, we have a cmdlet called Get-AzureVM. If you are properly authenticated, you can get a list of all virtual machines that are tied to your subscription.

With a magic wave of my wand, I cast the magical cmdlet:


Image of command output

Now we need to note the state of the virtual machine is different on the eyes. In Hyper-V, I would see a virtual machine that is operating like this:

Image of command output

In Hyper-V, it shows up as Running. In Azure, it shows up as ReadyRole. But filtering is similar. In Azure, if I need to show the virtual machines that are running, I run this command:

Get-AzureVM | where { $_.Status –eq 'ReadyRole' }

This will produce only the virtual machines that are operating. I can now pipe this directly to a cmdlet from Azure called (Oh! Hello, Captain Obvious!) Stop-AzureVM:

Get-AzureVM | where { $_.Status –eq 'ReadyRole' } | Stop-AzureVM

Odds are that you don't want to shut down your entire Azure infrastructure in one command. You're probably trying to shut down a single virtual machine (or a set of virtual machines).

In that case, target the name provided by the Get-AzureVM cmdlet. In our example, it's called 'brandnew1'.

There's one more piece. We need to tell Azure which Azure service we are targeting. Remember, you can identify your current service by using the Get-AzureService cmdlet:


You can then stop that virtual machine by its name and Azure service, for example:

Stop-AzureVM –Name 'brandnew1' –service $service

This will eventually yield a state of 'StoppedDeallocated', which means that you have the configuration and data from your C: partition, but the virtual machine is no longer active, consuming CPU time or active monitoring within the Azure environment.

You would think 'StoppedDeallocated' and 'ReadyRole' are the only two states to consider. But this is not the case.

Let's start the previous virtual machine in Azure and review the states it reveals via the Get-AzureVM cmdlet.

To start a virtual machine in Azure, we use the Start-AzureVM cmdlet and provide the virtual machine name and Azure service name. It's identical to Stop-AzureVM in its use.

Start-AzureVM –name 'Brandnew1' –service $service

As the virtual machine is starting up in Azure, note the two additional states it yields. You can see a Status of CreatingVM. In the management portal, this would be displayed as "Starting (Provisioning)":

Image of command output

It will be followed by "StartingVM" (normally viewed as simply "Starting" in the management portal. At this point, the virtual machine has been created and the internal operating system is starting:

Image of command output

When the process is complete, the virtual machine will return to its normal state of 'ReadyRole'.

A third condition to be aware of is when the computer has received a shutdown command within the operating system. In this condition, the resources are not deallocated within Azure and the virtual machine is not in a 'ReadyRole' state. Its status will show up as a 'StoppedVM':

Image of command output

This is going to be quicker to start; but be aware that in this state, your virtual machine is still pulling money on your account and you are being billed for it. If you'd like to turn it off properly, run Start-AzureVM. If you'd like to save some money, use Stop-AzureVM.

Pretty simple.

If you're trying to pull information on using the Azure cmdlets, I invite you to check out the Microsoft Azure PowerShell Reference Guide on Michael Washam's blog (he is the author of the cmdlets). In addition, there are some excellent posts from Keith Mayer at Microsoft on the subject, including Microsoft Azure Virtual Machines: Reset Forgotten Admin Passwords with Windows PowerShell.

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 to eat your vmdlets every day with a dash of creativity.

Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy


Discussion is closed.

Feedback usabilla icon