Skip to content

Instantly share code, notes, and snippets.

@dpo007
Last active March 10, 2022 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpo007/83e66da8a5729cbe599787553f0d47ca to your computer and use it in GitHub Desktop.
Save dpo007/83e66da8a5729cbe599787553f0d47ca to your computer and use it in GitHub Desktop.
PowerShell :: Creates a self-deleting task that reboots Windows at midnight.
<#
Create self-deleting task that reboots the computer at midnight.
#>
$midnight = (Get-date -Hour 0 -Minute 0 -Second 0).AddDays(1)
$taskname = 'Reboot at midnight'
$taskdescription = 'Reboots computer at midnight.'
if (Get-ScheduledTask -TaskName $taskname -ErrorAction SilentlyContinue) {
Unregister-ScheduledTask -TaskName $taskname -Confirm:$false
}
Register-ScheduledTask -TaskName $taskname -InputObject (
(
New-ScheduledTask -Action (
New-ScheduledTaskAction -Execute 'shutdown.exe' -Argument '/r /f /t 120'
) -Trigger (
New-ScheduledTaskTrigger -Once -At $midnight
) -Settings (
New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 20) -DeleteExpiredTaskAfter (New-TimeSpan -Minutes 2) -AllowStartIfOnBatteries
) -Principal (
New-ScheduledTaskPrincipal -UserId 'System' -RunLevel Highest
)
) | % { $_.Triggers[0].EndBoundary = $midnight.AddMinutes(2).ToString('s') ; $_ } # Run through a pipe to set the end boundary of the trigger
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment