Get-ShutdownExample.ps1


Description

Purpose

Displays an example PowerShell script for scheduling remote server shutdowns.

Detailed Description

This function outputs a sample script showing how to import servers from a CSV and schedule shutdowns with error handling.

Back to Top

Usage

Example 1

Get-ShutdownExample

Displays the shutdown scheduling example code in the console.

Back to Top

Notes

No additional notes.

Back to Top


Script

function Get-ShutdownExample {
    <#
    .SYNOPSIS
        Displays an example PowerShell script for scheduling remote server shutdowns.
    
    .DESCRIPTION
        This function outputs a sample script showing how to import servers from a CSV
        and schedule shutdowns with error handling.
    
    .EXAMPLE
        Get-ShutdownExample
        Displays the shutdown scheduling example code in the console.
    #>

    [CmdletBinding()]
    param()

    $scriptBlock = @'
# Import server list from CSV
$results = Import-Csv -Path "C:\GitRepos\ShutdownServers.csv"

# Array to store failed shutdowns
$failedShutdowns = @()

# Schedule shutdown for each server
foreach ($entry in $results) {
    $computer = $entry.ServerName
    try {
        Start-RemoteComputerShutdownSchedule -ComputerName $computer `
            -ShutdownTime (Get-Date -Date "16:00") `
            -Action "Shutdown" `
            -Comment "Scheduled shutdown at 4pm" `
            -Force
        Write-Host "Shutdown scheduled for $computer"
    }
    catch {
        Write-Warning "Failed to schedule shutdown for ${$computer}: $_"
        $failedShutdowns += $computer
    }
}

# Report failed shutdowns
if ($failedShutdowns.Count -gt 0) {
    Write-Host "`nThe following servers failed to schedule shutdown:"
    $failedShutdowns | ForEach-Object { Write-Host "- $_" }
} else {
    Write-Host "`nAll shutdowns scheduled successfully."
}
'@

    $scriptBlock
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top