Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On multiple (about 20) servers, there exists the same directory C:\Deployments. This directory has multiple subfolders which contain more subfolders and files. The C:\Deployments directory itself does not contain files.

I have a batch file that, when run locally, cleans out the C:\Deployments directory. That is, it deletes all subfolders and their files, but doesn't delete the C:\Deployements directory itself.

The batch file code is:

echo off

set CAT=c:\deployments

dir "%%CAT%%"/s/b/a | sort /r >> %TEMP%\files2del.txt 
for /f "delims=;" %%D in (%TEMP%\files2del.txt) do (del /q "%%D" & rd "%%D") 
del /q %TEMP%\files2del.txt

My question is this:

How do I run this batch file on each server from a localized window? That is, how do I run one batch file and have these commands execute on every server simultaneously?

Thanks for any help/ideas!

share|improve this question

migrated from Nov 11 '11 at 9:31

This question came from our site for professional and enthusiast programmers.

Seems like a question for – Uwe Keim Nov 10 '11 at 18:41
I'm not familiar with these sites. Is a better place to ask about batch files? – user1040362 Nov 10 '11 at 18:51
Question about writing batch files: stackoverflow. Question about deploying batch files to 20 computers: serverfault (if domain) or superuser (if non-domain). – Raymond Chen Nov 10 '11 at 19:36

I don't have a Windows machine anymore, but used to do this stuff at work. There is psexec which allows you to remote to another server. You can also look into powershell remoting.

Powershell Remoting see

Using Dos / Batch, see

If you want them to run simultaneously, you can do this, by running jobs in powershell, see the same ss64 site.

If you are interested...

If your servers are listed in active directory, you can do a little trick where you say loop through computers using Active Directory

dsquery computer -limit 0 | ?{$_ -imatch "CN=([^,]+),"} | % {
  #remotely run your script here through ps remote

or replace dsquery... with gc $your_file_name containing list of files. Dos/Batch - my syntax may be off, but this is a nice way to loop over computers

for /f "eol=, tokens=1, delims=" %cc in ('dsquery computer -name -limit 0 ^| find /i /r /c:'"CN=([^,]+),"') do (

  @REM cannot recall how i used to get back references in dos like this, so may be off.
  @REM call your script here, passing your batch program to psexec
  set computer=%%c
  echo computer=!computer!

or replace dsquery... with type "%your file name%"

Also, if you ever run into a problem setting variables within a loop in batch where they do not persist as you would expect, place setlocal enabledelayedexpansion at the top of your program, and evaluate your variables like this *!var_name!* instead of %var_name%

share|improve this answer

I would like to suggest the WinRM/WinRS (Windows Remote Management/Windows Remote Shell) combination which requires a little bit of setup beforehand, but uses tools already built into Windows.

Once you have the setup complete, your script will need to change to include the winrs command to connect. You could create a list of servers in another text file and wrap another FOR loop around your current script to avoid having 20 lines that all do the same thing.

share|improve this answer

Frankly kikuchiyo's answer is awesome and I have voted as much. However for completeness sake I will suggest that you could map to each server and delete the files via a remote share.

I am assuming a domain administrator here, or at least an account that has admin on each of the machines.

But basically I would do womething like this (keeping your code to do the work, not how I woud have perosnally done it but that doesn't mean I'm right)

servers.txt is just a list of the server hostname/IP address

for /f %%a in (servers.txt) do :process %%a


set SERVER=%1


NET USE T: \%SERVER%\C$\Deployments

set CAT=T:

dir "%%CAT%%"/s/b/a | sort /r >> %TEMP%\files2del.txt

for /f "delims=;" %%D in (%TEMP%\files2del.txt) do (del /q "%%D" & rd "%%D")

del /q %TEMP%\files2del.txt


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.