Defrag all computers on a domain, for free, using PsTools

Computer Stuff

I’m going to enlighten my readers on a little “how-to” to get your entire domain’s hard drives defragged at once.  It’s pretty simple, and can be scheduled to run by windows task scheduler.  *note- It sometimes doesn’t work if the computer isn’t logged on by a user (for some unknown reason). Usually, if someone is using the computer, or has it locked, it runs perfectly.*

Start by downloading PsTools from Microsoft’s Website:

Once you’ve gotten that file, you can extract them into a folder where you keep the rest of your tools, but also copy them to your server’s (or whatever computer you want to run this on, but preferably a server) “c:\windows\system32” directory so that these tools can be run from the command-line from any starting directory.

Now, open a command prompt, and do a “netview >> list.txt” and hit enter (if they all don’t show up, you can do a scan in active directory for all computers, and type up your own list).

You’ll want to open that file, and remove all of the slashes from the computer names, and also the computer descriptions.  Basically,  we just want to have computer names in one column only.  I don’t think you have to delete all of the trailing spaces, but it would be a good practice to do so.  Also, if you don’t want to defrag all of the servers at the same time (probably not a good idea) I’d remove them as well, and create a separate text file for them, and also a separate scheduled task (we’ll do this later).

Now, once your list is complete, we can now invoke the psexec command and pipe the list.txt into it to test.  Here is an example that will defrag the C: of all of the computers in the list:
psexec -d @list.txt defrag.exe c: -f

You may also want to add the /accepteula switch the first time that you run the tool, or just click accept once it pops up. This only has to be done once. You can also put the task into backgroud priority on Vista systems so that it’s not so invasive.  Add the -background switch after the, “-d”.

Now, you’ll see that psexec runs the defrag.exe comand with your switches C: -f.  Now, if you have computers that have a d: or others you can add that into another task if needed.

After you see it work, we can create a scheduled task to make this thing work anytime you want.

Create a batch file (call it whatever you want) and put that line in the file: psexec -d @list.txt defrag.exe c: -f

Save it somewhere, AS WELL AS copy your list.txt file into the same folder, and now open your scheduled tasks on your computer (server).  Schedule it to run the batch file as much as you want (maybe every 2 weeks or just once a month).  Be sure to put in a domain admin username and password as the psexec command will run under whatever username called for it, and it must have administrative rights on the client computer to which you are running the program on.

There are also many other things you can run with the psexec program, as well as the other tools that are a part of PsTools, so experiment all you want, but be careful, because they are very powerful tools…

Leave a Reply