Following up on what @Lee_Dailey, and @Jeroen Mostert commented. You can set a daily scheduled task to run at certain times if needed. Luckily, powershell has the New-SchduledTask cmdlet you can use to easily do this.
Creating a New Scheduled Task
A scheduled task requires a minimum of two components to work; action and trigger.
- Action – the action that is executed by the scheduled task. An action is typically to run a program or a script. A scheduled task can have more than one actions.
- Trigger – controls when the scheduled task runs. Triggers can be time-based, like, setting a schedule for daily or hourly recurrence. Triggers can also be activity-based, which runs a task based on detected activities like computer startup, a user logs in, or logged events.
Looking at the various cmdlets returned for after running Lee's suggestion: Get-Command *scheduled*, will return 3 specific cmdlets that we would need to work with in your case.
New-ScheduledTaskAction
New-ScheduledTaskTrigger
Register-ScheduledTask
New-ScheduledTaskAction
Using New-ScheduledTaskAction, we will create a scheduled task action objet and store it in a variable; $taskAction. The action will call Powershell.exe and pass the argument to run the Stop-Computer cmdlet shutting down your computer.
# Create a new task action
$taskAction = New-ScheduledTaskAction `
-Execute 'powershell.exe' `
-Argument 'Stop-Computer -Force'
$taskAction
New-ScheduledTaskTrigger
Next up, is the New-ScheduledTaskTrigger cmdlet which we will use to create a trigger to execute the scheduled-task at 23:00 daily.
#Create task trigger
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 9PM
$tasktrigger
Register-ScheduledTask
Finally, we can schedule the task using Register-ScheduledTask to peace it all together.
# Register the new PowerShell scheduled task
# The name of your scheduled task.
$taskName = "Shutdown Computer"
# Describe the scheduled task.
$description = "Shuts computer down daily at 2300"
# Register the scheduled task
Register-ScheduledTask `
-TaskName $taskName `
-Action $taskAction `
-Trigger $taskTrigger `
-Description $description
All coming together like such:
# The name of your scheduled task.
$taskName = "Shutdown Computer"
# Describe the scheduled task.
$description = "Shuts computer down daily at 2300 MTN"
# Create a new task action
$taskAction = New-ScheduledTaskAction `
-Execute 'powershell.exe' `
-Argument 'Stop-Computer -Force'
#Create task trigger
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 9PM
# Register the new PowerShell scheduled task
# Register the scheduled task
Register-ScheduledTask `
-TaskName $taskName `
-Action $taskAction `
-Trigger $taskTrigger `
-Description $description
This is for future readers that may be asking the same question on how this can be done. All credits go to Author June Castillote who wrote the article Powershell Scheduled Task in which I pulled the code from a long with the explanations for this post (just summarized).
For the help of any of these cmdlets, Get-Help cmdlet-name -Online can be run to get the most up to date help for any of the ones mentioned here, along with the full syntax and other optional parameters that may be used; like adding in what context it should be run in using -Principal.