Command Line Interface (CLI)

PowerShell

PowerShell

Pipe Cmdlets

Format-Table

# Long syntax
Get-AzSubscription | Format-Table
Get-AzSubscription | Format-Table -Property Name, Id

# Short syntax
Get-AzSubscription | ft
Get-AzSubscription | ft Name, Id

Format-List

# Long syntax
Get-AzSubscription | Format-List
Get-AzSubscription | Format-List -Property Name, Id

# Short syntax
Get-AzSubscription | fl
Get-AzSubscription | fl Name, Id

Where-Object

# Long syntax
Get-AzSubscription | Where-Object { $_.Id -eq 'xxxx' }

# Short syntax
Get-AzSubscription | ? { $_.Id -eq 'xxxx' }

ConvertTo-Json

Get-AzSubscription | ConvertTo-Json

Out-File

Get-AzSubscription | ConvertTo-Json | Out-File "subscription.json"
PowerShell

Stop Multiple VMs in Multiple Resource Groups

List All VMs

Get-AzVM -ResourceGroupName 'az104-06*' -Status
ResourceGroupName         Name      Location       VmSize  OsType           NIC Provisioning Zone PowerState MaintenanceAllowed
-----------------         ----      --------       ------  ------           --- ------------ ---- ---------- ------------------
AZ104-06-RG1      az104-06-vm0 southeastasia Standard_B1s Windows az104-06-nic0    Succeeded      VM running
AZ104-06-RG1      az104-06-vm1 southeastasia Standard_B1s Windows az104-06-nic1    Succeeded      VM running
AZ104-06-RG2      az104-06-vm2 southeastasia Standard_B1s Windows az104-06-nic2    Succeeded      VM running
AZ104-06-RG3      az104-06-vm3 southeastasia Standard_B1s Windows az104-06-nic3    Succeeded      VM running

Stop All VMs

Get-AzVM -ResourceGroupName 'az104-06*' | Stop-AzVM -Force -AsJob

Get job statuses

Get-Job
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      PowerShell.OnI…                 Stopped       False                                …
2      Long Running O… AzureLongRunni… Running       True            localhost            Stop-AzVM
3      Long Running O… AzureLongRunni… Running       True            localhost            Stop-AzVM
4      Long Running O… AzureLongRunni… Running       True            localhost            Stop-AzVM
5      Long Running O… AzureLongRunni… Running       True            localhost            Stop-AzVM

Wait until all jobs complete.

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      PowerShell.OnI…                 Stopped       False                                …
2      Long Running O… AzureLongRunni… Completed     True            localhost            Stop-AzVM
3      Long Running O… AzureLongRunni… Completed     True            localhost            Stop-AzVM
4      Long Running O… AzureLongRunni… Completed     True            localhost            Stop-AzVM
5      Long Running O… AzureLongRunni… Completed     True            localhost            Stop-AzVM

Check status again

ResourceGroupName         Name      Location       VmSize  OsType           NIC Provisioning Zone     PowerState MaintenanceAllowed
-----------------         ----      --------       ------  ------           --- ------------ ----     ---------- ------------------
AZ104-06-RG1      az104-06-vm0 southeastasia Standard_B1s Windows az104-06-nic0    Succeeded      VM deallocated
AZ104-06-RG1      az104-06-vm1 southeastasia Standard_B1s Windows az104-06-nic1    Succeeded      VM deallocated
AZ104-06-RG2      az104-06-vm2 southeastasia Standard_B1s Windows az104-06-nic2    Succeeded      VM deallocated
AZ104-06-RG3      az104-06-vm3 southeastasia Standard_B1s Windows az104-06-nic3    Succeeded      VM deallocated

References

Bash