Restart a computer at a given time with PowerShell

2

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

Jente

Posted 2014-12-02T14:26:36.397

Reputation: 356

1you can use schtasks.exe to schedule a task - from any (power)shell. – marsh-wiggle – 2014-12-02T14:45:12.063

I'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. – Tanner Faulkner – 2014-12-02T18:02:03.973

I'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. – Jente – 2014-12-03T07:07:50.633

Answers

2

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.

saltface

Posted 2014-12-02T14:26:36.397

Reputation: 141