Check Azure VM status with PowerShell

The Get-AzureRmVM PowerShell cmdlet leaves a bit to be desired. Learn how to obtain the detailed status of your Windows Server and Linux virtual machines (VMs) in Azure by using Azure PowerShell and some administrative scripting skills.
Latest posts by Timothy Warner (see all)

In today's tutorial, I assume you have already installed and configured Azure PowerShell on your administrative workstation. Our goal is simple: we need "at-a-glance" details concerning the running state of all VMs in our current Azure context.

First, you should open an elevated PowerShell session. Second, you should authenticate to Azure by running Add-AzureRmAccount or its alias, Login-AzureRmAccount. Third, you should run Set-AzureRmContext to ensure you're using the proper Azure subscription.

Now we're ready to get down to business!

Get-AzureRmVM ^

The default output of the Get-AzureRmVM cmdlet is okay, but it doesn't give you the running state by default. Check out the following screenshot:

Get AzureRmVm default output

Get AzureRmVm default output

We can do better than that! Let's start our experimentation by creating a variable named $dc to hold a reference to my dc VM:

That -Status switch parameter is actually crucially important, by the way. Now then: if we pipe that variable to Get-Member, we can examine the property data exposed by the PSVirtualMachineInstanceView object:

Enumerate the properties of the PSVirtualMachineInstanceView object

Enumerate the properties of the PSVirtualMachineInstanceView object

If you left off -Status in the $dc variable declaration and then piped it to Get-Member, you'd see the properties associated with the PSVirtualMachine object, which doesn't give us enough detail.

Enumerate the properties of a PSVirtualMachine object

Enumerate the properties of a PSVirtualMachine object

The property we're after here is DisplayStatus, which is tucked underneath the Statuses property. Here, let me demonstrate:

Are you with me so far? Let's write a simple PowerShell function to give us the run state of all VMs in our current subscription context.

Our script ^

Fire up Visual Studio Code, the PowerShell ISE, or your favorite code editor, and add the following function definition. (For your convenience, you can get the source code by viewing my GitHub Gist.) In this example, I adapted my Pluralsight colleague John Savill's code from an article.

Let me explain each code line so you understand exactly what's happening:

  • 1: I've defined a simple PowerShell function here, but you can make the function much more powerful by making it an advanced function.
  • 3: Enumerate all resource groups in the current subscription context.
  • 4: Loop through each resource group.
  • 6: Define a collection of all VMs across all resource groups.
  • 7: Loop through each VM in the collection.
  • 9: Obtain the status data for each VM.
  • 10: Grab the resource group name for each VM.
  • 11: Loop through the status information for each VM.
  • 13: Obtain the specific runtime status property information.
  • 15: Print the output to the screen in a reader-friendly format.

You can see my function results in the following screenshot. Note that I have one running VM and another one shut down and deallocated.

Function results

Function results

Extra credit : Obtain Azure VM uptime ^

What if we want to determine how long a particular Windows Server VM in Azure has been up? We can use PowerShell and Azure PowerShell to solve this problem as long as the VM is enabled for WinRM/WS-Man remoting and your network security group (NSG) has an exception for TCP 5985 and 5986.

Given that, we can start by defining a PowerShell remoting session object, specifying the VM's public IPv4 address, and authenticating with a local administrator account:

Next, we obtain a reference to the system's Win32_OperatingSystem CIM class:

Notice in the previous code we plug in our $sess session data. Once there, we can pluck CIM data from the Azure VM seamlessly.

Given that action, we can perform simple subtraction from the current date/time to get the system uptime; the system's last startup time is stored in the LastBootUpTime property:

Lastly, we can use simple string concatenation to display our output:

The output will look something like the following:

Your homework assignment is to integrate this uptime routine into the original Get-ARMVM code. Feel free to put your results in the comments so we can all benefit. Happy coding!


Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

  1. HelloFellow 3 years ago

    Whats the python equivalent of this to get the status of a vm?


  2. Andy McAdam 8 months ago

    This is a great article and had helped me. But I need to also gather details on patch information on ym VMs, namely: when patches were last installed, what the last critical patch was, when the last update scan was, and what patches available for install.

    My issue is I won't always know which log analytics workspace each vm is connected to, so having to try to work back from using search-azgraph queries in powershell across all my subs.


    • Get-Hotfix commandlet will give you basic overview. But it does not show criticality and last scan status.



      • Or

        Im not sure whether the criticity of the updates is stored is Windows client itself, its most probably only in WSUS or SCCM.


  3. Maykon 7 months ago

    You can get the status of your VM with the command:

    Get-AzVm | Get-AzVm -Status | select ResourceGroupName, Name, @{n="Status"e={$_.Statuses[1].DisplayStatus}}

    I think the -Status is avaliable just for AZ module.



Leave a reply

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


© 4sysops 2006 - 2020


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


Log in with your credentials


Forgot your details?

Create Account