Last active
March 10, 2022 21:07
-
-
Save dpo007/83e66da8a5729cbe599787553f0d47ca to your computer and use it in GitHub Desktop.
PowerShell :: Creates a self-deleting task that reboots Windows at midnight.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
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