PowerShell task scheduling: restart computer or server

 

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

Plan the reboot on another computer or server

Invoke-Command -ComputerName "FQDNcomputer.domain.tld" -ScriptBlock {
    $action = New-ScheduledTaskAction -Execute 'c:\windows\system32\shutdown.exe' -Argument '-r -t 300' 
    $tomorrow = (Get-Date -Hour 0 -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
}   #-Credential get-credential

Optional -Credential get-credential asks for an alternative User on the Server

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!

Updated: 2023-02-28 von Bernhard | Übersetzung Deutsch |🔔 | Comments:0

Restore file version history | PowerShell | PowerShell: Read Active Directory data

Top articles in this section


PowerShell: Prevent screen saver, lock: Move mouse regularly

Alternatively, if you can't change the screen lock settings, you can move the mouse regularly, or have a script move the mouse. Originally published as an AutoIt script, I recreated the script with a few PowerShell lines. Anyone who copies the following commands into a PowerShell session will prevent the computer from locking the screen or starting the screensaver:


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 TCP Listener

PowerShell uses the following commands to open a socket on a specific port via System.NET:

Questions / Comments