All scripts
Automation 135

Start-AzureVMScheduledShutdown

Sets or updates the auto shutdown schedule on every VM in a resource group, useful for enforcing a consistent shutdown time across dev and test environments to cut cost.

Start-AzureVMScheduledShutdown.ps1
<#
.SYNOPSIS
    Configures auto shutdown schedules across VMs in a resource group.

.DESCRIPTION
    Applies a consistent daily auto shutdown time to every VM in a given
    resource group, useful for enforcing cost control on dev and test
    environments where machines often get left running overnight.

.PARAMETER ResourceGroupName
    Resource group containing the target VMs.

.PARAMETER ShutdownTime
    24 hour time to shut down, in HHmm format. Defaults to 1900.

.PARAMETER TimeZone
    Time zone for the schedule. Defaults to "UTC".

.EXAMPLE
    .\Start-AzureVMScheduledShutdown.ps1 -ResourceGroupName rg-dev -ShutdownTime 2000

.NOTES
    Requires the Az.Compute and Az.Resources modules and an active
    Connect-AzAccount session.

.AUTHOR
    Shehryar Hassan
#>

param(
    [Parameter(Mandatory)]
    [string]$ResourceGroupName,
    [string]$ShutdownTime = "1900",
    [string]$TimeZone = "UTC"
)

$vms = Get-AzVM -ResourceGroupName $ResourceGroupName

foreach ($vm in $vms) {
    $properties = @{
        status     = "Enabled"
        taskType   = "ComputeVmShutdownTask"
        dailyRecurrence = @{ time = $ShutdownTime }
        timeZoneId = $TimeZone
        targetResourceId = $vm.Id
    }
    New-AzResource -ResourceId "$($vm.Id)/providers/microsoft.devtestlab/schedules/shutdown-computevm-$($vm.Name)" -Properties $properties -Force | Out-Null
    Write-Host "Configured auto shutdown at $ShutdownTime $TimeZone for $($vm.Name)" -ForegroundColor Cyan
}

Read it before you run it, and test in a safe tenant first.