Functions/Shutdown-Timer.ps1
function Shutdown-Timer { [CmdletBinding()] param ( [parameter()] [int] $Time = 4, [Parameter()] [string] [ValidateSet("Seconds", "Minutes", "Hours")] $HMS = "Seconds", [parameter()] [string] $At ) switch ($HMS) { "Hours" { $Seconds = ($time * 60 * 60) } "Minutes" { $Seconds = ($time * 60) } "Seconds" { $Seconds = $time } } if ($At) { if($At.Length -eq 3) { # $At $At = "0$($At)" $At } elseif($At.Length -eq 4) { } else { Write-Error "`"At`" not in correct format" } $ShutdownHour = $At[0] + $At[1] $ShutdownHour = [int]$ShutdownHour # $ShutdownHour $ShutdownMinute = $At[2] + $At[3] $ShutdownMinute = [int]$ShutdownMinute # $ShutdownMinute if($ShutdownHour -gt 23 -or $ShutdownMinute -gt 59) { Write-Error "Time input not correct" break } $EndTime = Get-Date -Hour $ShutdownHour -Minute $ShutdownMinute -Second 00 if($EndTime -lt (Get-Date)) { $EndTime = $EndTime.AddDays(1) } } else { $EndTime = (Get-Date).AddSeconds($Seconds) } while (($TimeRemaining = ($EndTime - (Get-Date))) -gt 0) { Write-Progress -Activity 'Shutting down in...' -Status $EndTime -SecondsRemaining $TimeRemaining.TotalSeconds Start-Sleep 1 } Stop-Computer -Force } |