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!

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details