Managing Azure VMs has become more convenient!

April 02, 2025

March 30, 2025

Description

I've been working with PowerShell again and developed a useful script that lists all active Azure VMs, allowing you to choose which ones to shut down—no more manual effort! Whether you need to stop one, several, or all of them, this script will make it easy.

Features

  • Lists all active VMs along with their resource groups – Get a quick overview in just a few seconds.
  • Allows selection by number (e.g., "1,3") or by typing "all" – Flexible and easy to use.
  • Handles errors smoothly – No one wants the script to crash halfway through.

Prerequisites

  • PowerShell installed
  • Azure PowerShell module installed
  • Active Azure subscription

  

  
  
# First, we need to log in to Azure
Connect-AzAccount

# Get all running VMs
$runningVMs = Get-AzVM -Status | Where-Object { $_.PowerState -eq "VM running" }

# Check if there are any running VMs
if ($null -eq $runningVMs -or $runningVMs.Count -eq 0) {
    Write-Host "No running virtual machines found in Azure." -ForegroundColor Yellow
} else {
    Write-Host "Found $($runningVMs.Count) running virtual machines:" -ForegroundColor Green
    
    # Create an array to store VMs the user wants to shut down
    $vmsToShutdown = @()
    
    # Display all VMs and let the user choose
    $index = 1
    foreach ($vm in $runningVMs) {
        $resourceGroup = $vm.ResourceGroupName
        $vmName = $vm.Name
        
        Write-Host "`n[$index] Virtual Machine: $vmName" -ForegroundColor Cyan
        Write-Host "Resource Group: $resourceGroup"
        $index++
    }
    
    # Ask which VMs to shut down
    Write-Host "`nEnter the numbers of the VMs you want to shut down (separate with commas, e.g., 1,3)" -ForegroundColor Yellow
    $choices = Read-Host "Your choices (or 'all' to shut down all)"
    
    # If user selects 'all', include all VMs; otherwise, process the selected numbers
    if ($choices.ToLower() -eq "all") {
        $vmsToShutdown = $runningVMs
    } else {
        $selectedNumbers = $choices -split ',' | ForEach-Object { $_.Trim() }
        foreach ($num in $selectedNumbers) {
            $idx = [int]$num - 1
            if ($idx -ge 0 -and $idx -lt $runningVMs.Count) {
                $vmsToShutdown += $runningVMs[$idx]
            }
        }
    }
    
    # Shut down the selected VMs
    foreach ($vm in $vmsToShutdown) {
        $resourceGroup = $vm.ResourceGroupName
        $vmName = $vm.Name
        
        try {
            Write-Host "Shutting down $vmName..." -ForegroundColor Yellow
            Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force -NoWait
            Write-Host "$vmName is being shut down." -ForegroundColor Green
        }
        catch {
            $errorMessage = $_.Exception.Message
            Write-Host "An error occurred while shutting down $vmName`: $errorMessage" -ForegroundColor Red
        }
    }
}

# Script completion message
Write-Host "`nScript completed!" -ForegroundColor Green
  

Follow my journey

Get my latest thoughts, posts and project updates in your inbox. No spam, I promise.