Azure PowerShell
The Az module manages Azure. Connect-AzAccount authenticates, and from there the cmdlets follow the usual Verb-Noun pattern with an Az prefix.
Documentation: Microsoft Learn
Overview
Context is the concept to get right first. Your session has a context: an account, a tenant and a subscription. Every command runs against that context, so a command that succeeds and does nothing visible has usually run against the wrong subscription. Get-AzContext before acting, and Set-AzContext -Subscription to switch. In a script, set it explicitly rather than inheriting whatever was last used.
The old AzureRM module is retired. Its cmdlets used an AzureRm prefix, and old examples still use them. Enable-AzureRmAlias exists as a migration aid, but new code should be Az.
Az is a meta-module of many sub-modules, so Install-Module Az pulls a lot. Install just Az.Accounts plus what you need, such as Az.Compute or Az.Storage, when size matters.
For automation, use a service principal or managed identity with Connect-AzAccount -Identity rather than an interactive login. Interactive login in a scheduled script fails at two in the morning by design.
Note that -Status on Get-AzVM is what populates PowerState; without it you get configuration and no running state.
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.
[pscustomobject]@{
Connect = 'Connect-AzAccount'
Context = 'Get-AzContext | Select-Object Name, Subscription, Tenant'
Select = 'Set-AzContext -Subscription "Production"'
Query = 'Get-AzVM -Status | Select-Object Name, ResourceGroupName, PowerState'
} | Format-List- Stage 1[pscustomobject]@{
Positional values or no named parameters - Stage 2Format-List
Positional values or no named parameters
What happens
This prints the sequence rather than connecting, since there is no network here. The order is the point: connect, check the context, set it deliberately, then query. Skipping the middle two steps is how a script deletes the right-named resource in the wrong subscription.
Worth remembering
- Every command runs against the current context. Set it explicitly in scripts.
- AzureRM is retired; write Az.
- Install Az.Accounts plus the sub-modules you need instead of all of Az.
- Get-AzVM needs -Status to report PowerState.
Primary documentation
Try it
Illustrative output · values vary by environment
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Prints the connect, check context, set context, query sequence.
Show reference output
Illustrative output · values vary by environment
Connect : Connect-AzAccount Context : Get-AzContext | Select-Object Name, Subscription, Tenant Select : Set-AzContext -Subscription "Production" Query : Get-AzVM -Status | Select-Object Name, ResourceGroupName, PowerState
Practise this
Read Azure VM status
An Azure read context is preloaded in this fictional lab. Return the fictional Azure VM status inventory.
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.
Inspect starting data
Supported syntax
Virtual files
Keep going
Next: PowerShell Background jobsCheck your understanding
A command finds no VMs although you can see them in the portal. What do you inspect first?
Inspect Get-AzContext for the account, tenant and subscription. Confirm the resource group and access before changing the query.