PowerShell Genie

Find stopped Azure VMs

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Filter Azure VM status data for stopped and deallocated machines. Keep the resource group and power state so the two conditions remain distinct.

The script

1PS> Get-AzVM -Status | Where-Object PowerState -match "stopped|deallocated" | Select-Object Name,ResourceGroupName,PowerState

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 ResourceGroupName Name Location PowerState
    2 ----------------- ----- ------------ --------------
    3 RG-DEV DEV01 centralindia VM deallocated
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-AzVM -Status | Where-Object PowerState -match "stopped|deallocated" | Select-Object Name,ResourceGroupName,PowerState
    1. Stage 1Get-AzVM-Status
    2. Stage 2Where-Object-match
    3. Stage 3Select-ObjectPositional values or no named parameters

      How it works

      The same -Status retrieval feeds a Where-Object that matches PowerState against a pattern covering both stopped and deallocated, and the projection keeps the name, resource group, and state so the two cases remain distinguishable in the result.

      Adapting it

      Tighten the pattern to match only the stopped state to isolate machines still incurring compute charges, since deallocated ones are already down to paying for their disks.

      Requirements

      Az.Compute with a Connect-AzAccount session.

      If the result is unexpected

      The stopped-VM report confuses stopped and deallocated machines.

      Provisioning state and runtime power state are different, and instance status may not have been requested.

      1. Retrieve VM status and inspect the PowerState value for a known machine.

      Validate the result

      Stopped and deallocated states are reported distinctly and match the status query.

      Keep the check reliable

      Record power-state definitions in reports that inform cost or restart decisions.

      Primary documentation

      PowerShell objects and pipelines documentation

      Practise the script’s building blocks

      Find deallocated VMs

      An Azure read context is preloaded in this fictional lab. Return Name and ResourceGroupName for deallocated VMs.

      Review the lesson

      Continue
      Genie guideFind stopped Azure VMs

      Filter Azure VM status data for stopped and deallocated machines. Keep the resource group and power state so the two conditions remain distinct.