13

Azure CLI allows people to start & stop VMs.

However, is there any Azure CLI command that can show the VM status? I.e., whether it is started or being stopped? Thx.

4 Answers 4

24

You can use the '-d' flag with list and status for all VMs

az vm list -d -o table

For single VM, you can use:

az vm list -d -o table --query "[?name=='vm-name']"

If you need more specific results then you can also try:

az vm list -d --query "[?powerState=='VM running']" -o table
5
  • 1
    That's indeed a better option than the accepted answer. Thanks & welcome to SU with my +25.
    – xpt
    Aug 13, 2019 at 12:47
  • 1
    I just love that az vm list gives me 174 lines of output per VM, including all sorts of esoteric settings that the doc barely explains, but then if you give it --show-details it only outputs a whole 7 extra lines! And that's where they decided to hide the VM state, because who could possibly want to know that!!
    – tux3
    Feb 1, 2022 at 0:13
  • Just an add on; If you are working with az cli and powershell it is help full to use convertfromjson and look at it that way instead of using table.
    – AndreasWBJ
    Sep 26, 2022 at 11:43
  • For single VM you can use az vm show -d instead. In both cases it's the -d (--show-details) that's important.
    – Jan Hudec
    Feb 20, 2023 at 16:23
  • show will list a whole bunch of information, a lot more than just the powerstate Feb 26 at 7:42
7

You can get the vm status with this command:

az vm get-instance-view --name vmName --resource-group resourceGroupName --query instanceView.statuses[1] --output table

The output will like this:

enter image description here

2

You can also run query in Azure Resource Graph Explorer

Resources
| project name, location, 
   PowerState=tostring(properties.extended.instanceView.powerState.code), type
| where type =~ 'Microsoft.Compute/virtualMachines'
| order by name desc
1

all these answers did not work for me I had to use

az vm show -d --resource-group my-rsg-name --name my-vm-name --subscription my-subsciption-id --query "powerState"

this just shows: enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .