Find stopped Azure VMs
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
Script reader
Inspect before you run
Inspect the script below. It does not run in this browser.
Inspection only — no command has run.
Show reference output
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- Stage 1Get-AzVM
-Status - Stage 2Where-Object
-match - Stage 3Select-Object
Positional 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.
- 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
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.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.