PowerShell task scheduling: restart computer or server

Diese Seite gibt es auch in Deutsch

A scheduled task in Windows can of course also be created via PowerShell.

cmd command

As an example, a task that restarts the computer once the following day at 3am:

$action = New-ScheduledTaskAction -Execute 'c:\windows\system32\shutdown.exe' -Argument '-r -t 300' 
$tomorrow = (Get-Date -Hour 3 -Minute 0 -Second 0 -Millisecond 0).AddDays(1)
$trigger = New-ScheduledTaskTrigger -Once -At $tomorrow
$settings = New-ScheduledTaskSettingsSet 
$user = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
Register-ScheduledTask -TaskName "planned Reboot" -TaskPath "\"  -Action $action -Settings $settings -Trigger $trigger -Principal $user

See also: www.libe.net/automatically-turn-off-windows

Starting a PowerShell script

A Powershell script can be scheduled as follows, in the following example daily 2:45am:

$action = New-ScheduledTaskAction -Execute '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"' -Argument '-ExecutionPolicy Bypass -File "C:\Windows\Disconnect-LoggedOnUsers.ps1"' 
$tomorrow = (Get-Date -Hour 2 -Minute 45 -Second 0 -Millisecond 0).AddDays(1)
$trigger = New-ScheduledTaskTrigger -Daily -At $tomorrow
$settings = New-ScheduledTaskSettingsSet 
$user = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
Register-ScheduledTask -TaskName "UserLogoff" -TaskPath "\"  -Action $action -Settings $settings -Trigger $trigger -Principal $user

see also how to create a Windows PowerShell script

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Publication: 2022-11-15 from Bernhard 🔔


Top articles in this section


PowerShell Log-Files: Logging into a textfile - write to file
Log files in PowerShell can be created via the Out-File command, via a custom function, or via PowerShell's built-in Transcript.

PowerShell Loops and Array
An array stores multiple values, similar to a 2-column table.

PowerShell text file and csv read / write
PowerShell text file and csv read / write

Questions / Comments