Use powershell to create a scheduled task to reboot computers on schedule
-
This thread reminded me I needed to update my process for forcibly rebooting user computers.
As I use GPO less and less as systems are switching to non-AD, I wanted to handle this with a scheduled task like I was pushing out with GPO.
This should be pretty self explanatory.
Create variables with the pieces of the command.
Then use the register command to create the task.#remove old task Unregister-ScheduledTask -TaskName "Weekly Reboot" -Confirm:$false # Create task action $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force' # Create a trigger (Mondays at 4 AM) $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am # The user to run the task $taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount # The name of the scheduled task. $taskName = "Weekly Reboot" # Describe the scheduled task. $description = "Forcibly reboot the computer at 4am on Mondays" # Register the scheduled task Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description
Once run, it will look like this:
-
@jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:
This thread reminded me I needed to update my process for forcibly rebooting user computers.
As I use GPO less and less as systems are switching to non-AD, I wanted to handle this with a scheduled task like I was pushing out with GPO.
This should be pretty self explanatory.
Create variables with the pieces of the command.
Then use the register command to create the task.# Create task action $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force' # Create a trigger (Mondays at 4 AM) $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am # The user to run the task $taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount # The name of the scheduled task. $taskName = "Weekly Reboot" # Describe the scheduled task. $description = "Forcibly reboot the computer at 4am on Mondays" # Register the scheduled task Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description
Good idea!
Maybe you should add a test to check if the task is already created?
That way you could use the same script to modify the time or whatever you wanted to do. Without having several tasks created I mean.
-
@pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:
@jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:
This thread reminded me I needed to update my process for forcibly rebooting user computers.
As I use GPO less and less as systems are switching to non-AD, I wanted to handle this with a scheduled task like I was pushing out with GPO.
This should be pretty self explanatory.
Create variables with the pieces of the command.
Then use the register command to create the task.# Create task action $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force' # Create a trigger (Mondays at 4 AM) $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am # The user to run the task $taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount # The name of the scheduled task. $taskName = "Weekly Reboot" # Describe the scheduled task. $description = "Forcibly reboot the computer at 4am on Mondays" # Register the scheduled task Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description
Good idea!
Maybe you should add a test to check if the task is already created?
That way you could use the same script to modify the time or whatever you wanted to do. Without having several tasks created I mean.
I added a remove
-
@jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:
@pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:
@jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:
This thread reminded me I needed to update my process for forcibly rebooting user computers.
As I use GPO less and less as systems are switching to non-AD, I wanted to handle this with a scheduled task like I was pushing out with GPO.
This should be pretty self explanatory.
Create variables with the pieces of the command.
Then use the register command to create the task.# Create task action $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force' # Create a trigger (Mondays at 4 AM) $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4am # The user to run the task $taskUser = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount # The name of the scheduled task. $taskName = "Weekly Reboot" # Describe the scheduled task. $description = "Forcibly reboot the computer at 4am on Mondays" # Register the scheduled task Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description
Good idea!
Maybe you should add a test to check if the task is already created?
That way you could use the same script to modify the time or whatever you wanted to do. Without having several tasks created I mean.
I added a remove
Remove if it's already created? That works.
EDIT: Yeah, I see it now. Unregister-ScheduledTask.
-
For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.
That way I know the tasks in there is not generated by something else.
-
@pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:
For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.
That way I know the tasks in there is not generated by something else.
But they did not do that for everything.
These tasks are all in the root\
path.
-
@pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:
For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.
That way I know the tasks in there is not generated by something else.
One could also fix the description to say the source of the task. I get the appeal of placing them in their own folder nested somewhere in that mess, but it's so much easier to deal with them in the top level directory.
-
@jaredbusch said in Use powershell to create a scheduled task to reboot computers on schedule:
@pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:
For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.
That way I know the tasks in there is not generated by something else.
But they did not do that for everything.
These tasks are all in the root\
path.
Yes, it's strange. Not consistent. But there are MANY more tasks under the Microsoft folder and it's sub folders.
-
@obsolesce said in Use powershell to create a scheduled task to reboot computers on schedule:
@pete-s said in Use powershell to create a scheduled task to reboot computers on schedule:
For logistics, I would also place scheduled tasks that I created in their own task folder. Just like Microsoft and others have.
That way I know the tasks in there is not generated by something else.
One could also fix the description to say the source of the task. I get the appeal of placing them in their own folder nested somewhere in that mess, but it's so much easier to deal with them in the top level directory.
I guess you could fix the name. But it's mostly misbehaving apps that put tasks in the top level directory. I mean Microsoft added the folder structure for task organization. It wasn't there in the old days.
But yeah, it works regardless.
-
This post is deleted!