Systems Administration

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”.

2 replies on “How To Schedule Remote Restarts For Windows PC's In A Specific OU – Server Basics”

Based on this article I created this script…
get-adcomputer -filter * -searchbase “OU=KMMRLS, OU=Computers, OU=Test, DC=KMMRLS, DC=COM”|Select * ,@{n=’computername’;e={$}} |restart-computer -force
It fails with the error get-adcomputer : Directory object not found.
I have verified that the module is loaded and these are OUs I have created. Any suggestions?

I looked at mine to make sure I had the script right on the site. I think the issue is how the AD/LDAP string is structured. I mess this up all the time. I’m putting this in comments because I think this is useful information.
We’ll use us.workedtech.Local as an even weirder fictional domain than in the article.
Say you put an OU under the default Computers and call it “Lab Stations” and then an OU under that and call it “Portland Accounting Lab”. You’ll structure your string like this: “OU=Portland Accounting Lab, OU=Lab Stations, CN=Computers, DC=us, DC=workendtech, DC=local”. Note that the default Computers,Users, Builtin and whatever are “Containers” and use “CN” not “OU” in the string. You can right click on the folder or object in your Active Directory window and hit properties, then click on the “Object” tab to see what it actually is.
You take the ‘folder’ furthest down in the chain and put it first in your string then work your way back up the folder structure to the domain name itself.
I think your string probably should be “OU=Test, OU=Computers, OU=KMMRLS, DC=KMMRLS, DC=COM” if I’m assuming correctly that you have an OU called KMMRLS, with an OU called Computers under it where you’ve put all your domain computers associated with KMMRLS, with a “Test” OU you made specifically for testing this script.

Leave a Reply

Your email address will not be published. Required fields are marked *