How To Schedule Remote Restarts For Windows PC’s In A Specific OU – Server Basics

Restarting a local server once after hours is a pretty neat trick but sometimes you need to restart an entire Organizational Unit of computers. There’s a few ways to do this. You can do it with the shutdown command by hostname and keep a little batch script updated. Or you can use a PowerShell Script.

I’d like to credit Jack McCarty for this command. He came up with most of this either on his own or from stuff he found various places on the internet.


How To Remotely Restart Computers In A Specific OU With Powershell

This is one of several methods and, honestly it’s pretty slow but it works pretty well, and the advantage is you can just add machines to the OU you want to restart. This is a pretty flexible command/script and can be modified to do other things instead of restarting computers. Also, please note this forces the restart.

Step 1 – Open up your favorite script editor on your server. I like Notepad++ personally. The Powershell ISE is pretty good as well.

Step 2 – Copy and paste the code snippet below into your code editor and change it. The appropriate place to modify is the string after -searchbase. You’ll need to change “OU=Lab, OU=Workstations, DC=workendtech, DC=local” to fit your environment. If your domain is say, and you wanted to reboot everything in the Computers CN you’d change it to something like “CN=Computers, DC=Example, DC=com”.

get-adcomputer -filter * -searchbase "OU=Lab, OU=Workstations, DC=workendtech, DC=local"|Select * ,@{n='computername';e={$}} |restart-computer -force

Step 3 – Save the file as a plain text file with a .ps1 file extension. Some code editors will have a Save As “PowerShell Script” file type that will do this do you.

Step 4 – Run the script through Powershell. You can also run the command directly without saving it as a script.

Scheduling A Powershell Script in Task Manager

There’s not much to scheduling a powershell script in task manager.

Step 1 – Create your Task as normal

Step 2 – In the Actions Window/Tab type “powershell” (without the quotes) in the ‘program/script’ box. In the arguments type “-file ” in the arguments box. You can also add the path to your script in the “Start In” ¬†box.

Note: Some scripts may need a few extra arguments. Testing the command in your command prompt will usually help you figure that out. The one above seems to work just fine with just “-file”.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.