PowerShell Azure Management

Azure PowerShell is a set of modules that let you manage Microsoft Azure cloud resources directly from the PowerShell command line. Instead of clicking through the Azure portal, you write commands to create virtual machines, manage storage accounts, configure networks, and deploy applications — all scriptable and repeatable.

How Azure PowerShell Works

+----------------------+
|   PowerShell Script  |
|  (Your commands)     |
+----------------------+
          |
          | Az Module sends API calls
          v
+----------------------+
|  Azure REST API      |
|  (Microsoft Cloud)   |
+----------------------+
          |
          | Provisions / Manages
          v
+----------------------+
|  Azure Resources     |
|  VMs, Storage, DBs,  |
|  Networks, etc.      |
+----------------------+

Installing the Az Module

# Install for the current user (no admin rights needed)
Install-Module -Name Az -Scope CurrentUser -Force

# Update an existing installation
Update-Module -Name Az

# Verify installation
Get-Module Az -ListAvailable | Select-Object Name, Version

Connecting to Azure

# Sign in interactively (opens browser for Microsoft login)
Connect-AzAccount

# Sign in with a specific tenant
Connect-AzAccount -TenantId "your-tenant-id"

# Sign in with a service principal (for automated scripts)
$credential = Get-Credential   # Enter App ID and Secret
Connect-AzAccount -ServicePrincipal -Credential $credential -TenantId "your-tenant-id"

# Check current connection
Get-AzContext

Sample Output of Get-AzContext

Name          : Visual Studio Subscription (abc123)
Account       : alice@company.com
Environment   : AzureCloud
Subscription  : abc123-def456-...
Tenant        : xyz789-...

Managing Subscriptions

# List all subscriptions your account has access to
Get-AzSubscription

# Switch to a different subscription
Set-AzContext -SubscriptionName "Production-Subscription"

# Or switch by subscription ID
Set-AzContext -SubscriptionId "abc123-def456-..."

Resource Groups

Resource groups are containers that hold related Azure resources. Every resource in Azure belongs to exactly one resource group.
# List all resource groups
Get-AzResourceGroup | Select-Object ResourceGroupName, Location

# Create a new resource group
New-AzResourceGroup -Name "MyApp-RG" -Location "East US"

# Delete a resource group (deletes ALL resources inside it)
Remove-AzResourceGroup -Name "MyApp-RG" -Force

Azure Resource Group as a Container

+-------------------------------------+
|  Resource Group: MyApp-RG           |
|  Location: East US                  |
|                                     |
|  +-----------+  +---------------+   |
|  |  VM       |  |  Storage Acct |   |
|  |  myapp-vm |  |  myappstorage |   |
|  +-----------+  +---------------+   |
|                                     |
|  +-----------+  +---------------+   |
|  |  SQL DB   |  |  Virtual Net  |   |
|  |  myapp-db |  |  myapp-vnet   |   |
|  +-----------+  +---------------+   |
+-------------------------------------+

Managing Virtual Machines

List Virtual Machines

# All VMs in the subscription
Get-AzVM | Select-Object Name, ResourceGroupName, Location

# VMs in a specific resource group
Get-AzVM -ResourceGroupName "MyApp-RG" | Select-Object Name, PowerState

Start, Stop, and Restart VMs

# Start a VM
Start-AzVM -ResourceGroupName "MyApp-RG" -Name "myapp-vm"

# Stop and deallocate (stops billing for compute)
Stop-AzVM -ResourceGroupName "MyApp-RG" -Name "myapp-vm" -Force

# Restart a VM
Restart-AzVM -ResourceGroupName "MyApp-RG" -Name "myapp-vm"

Create a Virtual Machine

# Define credentials
$cred = Get-Credential -Message "Enter VM admin username and password"

# Create the VM
New-AzVM `
    -ResourceGroupName "MyApp-RG" `
    -Name "myapp-vm" `
    -Location "East US" `
    -Image "Win2022Datacenter" `
    -Size "Standard_B2s" `
    -Credential $cred `
    -OpenPorts 3389   # Open RDP port

Write-Output "VM created successfully."

Get VM Status

Get-AzVM -ResourceGroupName "MyApp-RG" -Name "myapp-vm" -Status |
    Select-Object -ExpandProperty Statuses |
    Where-Object Code -like "PowerState*" |
    Select-Object DisplayStatus

Managing Storage Accounts

# Create a storage account
New-AzStorageAccount `
    -ResourceGroupName "MyApp-RG" `
    -Name "myappstorage2025" `
    -Location "East US" `
    -SkuName "Standard_LRS" `
    -Kind "StorageV2"

# Get storage account key
$key = (Get-AzStorageAccountKey -ResourceGroupName "MyApp-RG" -Name "myappstorage2025")[0].Value

# Create a storage context
$ctx = New-AzStorageContext -StorageAccountName "myappstorage2025" -StorageAccountKey $key

# Create a blob container
New-AzStorageContainer -Name "backups" -Context $ctx -Permission Blob

# Upload a file to blob storage
Set-AzStorageBlobContent `
    -File "C:\Reports\report.pdf" `
    -Container "backups" `
    -Blob "report.pdf" `
    -Context $ctx

Common Azure PowerShell Modules

ModuleWhat It Manages
Az.ComputeVirtual machines, disks, availability sets
Az.StorageStorage accounts, blobs, queues, tables
Az.NetworkVirtual networks, subnets, NSGs, load balancers
Az.ResourcesResource groups, ARM templates, tags
Az.SqlAzure SQL databases and servers
Az.KeyVaultSecrets, keys, certificates in Key Vault
Az.AccountsAuthentication, subscriptions, contexts
Az.WebSitesApp Service web apps and plans

Tagging Resources

Tags are key-value labels attached to Azure resources for cost tracking, automation, and organization.
# Tag a resource group
Set-AzResourceGroup -Name "MyApp-RG" -Tag @{
    Environment = "Production"
    Owner       = "Alice"
    CostCenter  = "IT-001"
}

# Find all resources with a specific tag
Get-AzResource -Tag @{Environment = "Production"} |
    Select-Object Name, ResourceType, ResourceGroupName

Real-World Example: Start All VMs in a Resource Group

function Start-AllVMs {
    param($resourceGroup)

    $vms = Get-AzVM -ResourceGroupName $resourceGroup

    foreach ($vm in $vms) {
        $status = (Get-AzVM -ResourceGroupName $resourceGroup -Name $vm.Name -Status).Statuses |
                  Where-Object Code -like "PowerState*"

        if ($status.DisplayStatus -ne "VM running") {
            Write-Output "Starting $($vm.Name)..."
            Start-AzVM -ResourceGroupName $resourceGroup -Name $vm.Name
            Write-Output "$($vm.Name) started."
        } else {
            Write-Output "$($vm.Name) is already running."
        }
    }
}

Start-AllVMs -resourceGroup "MyApp-RG"

Disconnecting from Azure

# Sign out of the current Azure session
Disconnect-AzAccount

Summary

Azure PowerShell brings the full power of cloud management into a script. The Az module covers virtual machines, storage, networking, databases, and more. Connect with Connect-AzAccount, work with resource groups as containers, and automate repetitive cloud tasks like starting VMs, uploading files, and tagging resources. Azure PowerShell is essential for DevOps engineers, cloud administrators, and anyone managing Azure environments at scale.

Leave a Comment

Your email address will not be published. Required fields are marked *