Restart a computer at a given time with PowerShell
You could run
shutdown -r -t ([decimal]::round(((Get-Date).AddDays(1).Date.AddHours(3) - (Get-Date)).TotalSeconds))
This will get the number of seconds between the time the script is ran and 3:00AM the following day. It then passes the result to shutdown.exe. Obviously this is designed to be ran before midnight.
Related videos on Youtube
Jente
Updated on September 18, 2022Comments
-
Jente over 1 year
I'm looking for a way to restart a computer with PowerShell (
Restart-Computer
), at a given time (Example: 03:00AM). The PS-Script itself will run at a random time, when the user executes it, so it's no option to schedule the script at 03:00AM.My first thought was to schedule a task, at 03:00AM (with PowerShell) that will execute the reboot for me. This is possible (I think) with
New-ScheduledTask
in PowerShell 4.0. The downside is that I'm not sure if the computers that will run this script have PowerShell 4.0.Is it possible to schedule a task with PowerShell 2.0, or is there another way to reboot the computer at a given time?
Thank you
-
marsh-wiggle over 9 yearsyou can use schtasks.exe to schedule a task - from any (power)shell.
-
rtf over 9 yearsI'm a little confused here. Are you trying to create the task using Powershell? That's what
New-ScheduledTask
is for. If you're just looking to set up a scheduled reboot, Scheduled Tasks (the GUI) is going to be your friend here. -
Jente over 9 yearsI'm looking for a way to schedule a reboot, with Powershell. New-ScheduledTask is only for PowerShell 4.0. I'm going to try Boboes his approach, thank you.
-