Check the version of PowerShell Installed

pwsh -ver

Install Azure CmdLet

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery

Change Execution Policy (if restricted)

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Update a the Azure Powershell Module

Update-Module -Name Az

Connect PowerShell to Azure Subscription

Connect-AzAccount
Get-AzSubscription
Set-AzContext -Subscription '00000000-0000-0000-0000-000000000000'

Enumerate Resources and Groups

Get-AzResourceGroup
Get-AzResourceGroup | Format-Table

Get-AzResource
Get-AzResource | Format-Table
Get-AzResource -ResourceGroupName <name>

Create a Resource Group

New-AzResourceGroup -Name <name> -Location <location>

VM Commands

# create
New-AzVm
       -ResourceGroupName <resource group name>
       -Name <machine name>
       -Credential <credentials object>
       -Location <location>
       -Image <image name>

# status of all VMs
Get-AzVM -Status

# get VM object into a variable
$vm = (Get-AzVM -Name <vm name> -ResourceGroupName <rg name>)

# output the $vm properties
$vm
$vm.HardwareProfile
$vm.StorageProfile.OsDisk

# get the public IP of a VM
Get-AzPublicIpAddress -ResourceGroupName <rg name> -Name <vm name>

Remove-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
Start-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
Restart-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
Update-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName

Get Credential

$adminCredential = Get-Credential

Place the Region in a variable

$loc = "East US"
New-AzResourceGroup -Name <rg name> -Location $loc

PowerShell Loops

For ($i = 1; $i -lt 3; $i++)
{
    $i
}