PowerShell Azure: Connect and Manage Resources with Az Module

Managing 200 Azure VMs, resource groups, and storage accounts through the portal is how you generate toil. The Az PowerShell module is your CLI control plane for Azure — every resource type, every operation, as cmdlets with structured output that pipelines cleanly into reports and automation. PowerShell Azure administration with the Az module covers everything from initial authentication through VM lifecycle management and ARM template deployment.
Install the Az Module
The Az module is available from the PowerShell Gallery and replaces the older AzureRM module. Do not install both — they conflict. If AzureRM is present, uninstall it first.
# Check for and remove conflicting AzureRM module
if (Get-InstalledModule -Name AzureRM -ErrorAction SilentlyContinue) {
Uninstall-AzureRm
}
# Install Az module
Install-Module -Name Az -Scope CurrentUser -AllowClobber -Force
# Verify installation
Get-InstalledModule -Name Az | Select-Object Name, Version
Connect-AzAccount Authentication
Connect-AzAccount opens a browser for interactive login with MFA support. For unattended scripts, use a service principal with a certificate or client secret.
# Interactive login
Connect-AzAccount
# Service principal with client secret (for automation)
$tenantId = "your-tenant-id"
$clientId = "your-service-principal-app-id"
$clientSecret = ConvertTo-SecureString "your-secret" -AsPlainText -Force
$credential = New-Object PSCredential($clientId, $clientSecret)
Connect-AzAccount -ServicePrincipal `
-TenantId $tenantId `
-Credential $credential
# Verify login
Get-AzContext
Name Account SubscriptionName TenantId
---- ------- ---------------- --------
Dev [email protected] Dev Subscription abc-123...
List Subscriptions and Switch Context
If your account has access to multiple subscriptions, list them and set the active context to avoid targeting the wrong subscription.
# List all accessible subscriptions
Get-AzSubscription | Select-Object Name, Id, State
# Set active subscription context
Set-AzContext -SubscriptionId "your-subscription-id"
# Alternatively, use the subscription name
Set-AzContext -Subscription "Production Subscription"
List and Filter Virtual Machines
Get-AzVM returns VM objects. Combined with Get-AzVM -Status, you get the current power state of each VM.
# All VMs in current subscription
Get-AzVM | Select-Object Name, ResourceGroupName, Location
# VMs in a specific resource group
Get-AzVM -ResourceGroupName "Prod-RG" | Select-Object Name, Location,
@{ N="Size"; E={ $_.HardwareProfile.VmSize } }
# VMs with their power state
Get-AzVM -Status | Select-Object Name, ResourceGroupName,
@{ N="PowerState"; E={ $_.PowerState } } |
Where-Object PowerState -eq "VM running"
Start, Stop, and Resize VMs
VM power operations run asynchronously by default. Add -Wait (or -NoWait:$false) to block until the operation completes.
# Start a VM
Start-AzVM -Name "WebServer01" -ResourceGroupName "Prod-RG"
# Stop (deallocate) a VM — stops billing for compute
Stop-AzVM -Name "WebServer01" -ResourceGroupName "Prod-RG" -Force
# Resize a VM
$vm = Get-AzVM -Name "WebServer01" -ResourceGroupName "Prod-RG"
$vm.HardwareProfile.VmSize = "Standard_D4s_v3"
Update-AzVM -VM $vm -ResourceGroupName "Prod-RG"
# Stop all VMs in a resource group (cost saving)
Get-AzVM -ResourceGroupName "Dev-RG" |
ForEach-Object { Stop-AzVM -Name $_.Name -ResourceGroupName $_.ResourceGroupName -Force -AsJob }
Deploy ARM Template from PowerShell
Deploy Azure resources from an ARM template JSON file using New-AzResourceGroupDeployment. Template parameters can be supplied inline or from a parameters file.
# Deploy a template to an existing resource group
New-AzResourceGroupDeployment `
-Name "AppDeployment-$(Get-Date -Format yyyyMMdd)" `
-ResourceGroupName "Prod-RG" `
-TemplateFile "C:\Templates\webapp.json" `
-TemplateParameterFile "C:\Templates\webapp.parameters.json"
# Check deployment status
Get-AzResourceGroupDeployment -ResourceGroupName "Prod-RG" |
Select-Object DeploymentName, ProvisioningState, Timestamp |
Sort-Object Timestamp -Descending | Select-Object -First 5
Common Errors and Fixes
- Az and AzureRM conflict: Both modules define cmdlets with the same patterns, and PowerShell will load whichever is imported first — leading to unpredictable behavior. Completely uninstall AzureRM with
Uninstall-AzureRm(a cmdlet provided by the AzureRM module itself) before installing Az. If the system-level removal fails, useGet-InstalledModule -Name AzureRM* | Uninstall-Module -AllVersions -Force. - Subscription context not set: Commands run against the wrong subscription when the default context points to a different one. After
Connect-AzAccount, always verify context withGet-AzContextand explicitly set the correct subscription withSet-AzContext -Subscription "name-or-id"before running any resource commands. In scripts that target a known subscription, set the context at the top of the script so it cannot be affected by previous session state.
Related Cmdlets / See Also
Wrapping Up
The Az module gives you a fully scriptable Azure management experience that scales from managing a handful of VMs to deploying full infrastructure as code. As a next step, build a cost management script that identifies VMs in non-production resource groups that are running during business hours and generates a daily report showing potential savings from scheduling shutdowns.


