Batch File Script on Multiple Computers over Network


scripting

Without using PowerShell, here’s an “old school” batch file script method of running a simple “For Loop” against multiple computers over a network.  As an example, my batch file includes a program called “PsExec” from Windows Sysinternals.  However, you could also use this simple batch file script with other programs such as Microsoft’s Robocopy or even xcopy.exe.  I think that you’ll find these few lines of code very applicable even in today’s computing environment.  Moreover, the Information Technology derivation of Occam’s razor, should one exist, probably states that when you have two competing tech solutions that produce exactly the same results, the simpler one is better.  You heard it first at the Caged Rat!

Related articles:
Install Adobe Flash on Multiple Computers


PsExec Download and Setup

  1. Download PsExec @ Windows Sysinternals
  2. Extract the PSTools.zip file to your c: drive
  3. Update your environment system variable “Path” with the location of the PSTools folder:
    • Go to System Properties => Click on Advanced tab => Click on Environment Variables…
    • In Environment Variables, System variables, click on “Path” => click on Edit…
    • In Edit System Variable, append your “PsTools” directory path to the end of the “Path” Variable value: ;c:\PsTools\

      Env_Var_1
      Env_Var_2


@ECHO OFF

REM This Batch Script uses a For Loop command to
REM run commands against multiple computers over the network.
REM Right-click on your batch file and click on
REM "Run as administrator"
REM PsExec uses "-h" to run commands with elevated
REM privileges due to Windows User Access Control (UAC).

For /f %%i in (c:\computers.txt) do (
Echo ************************
Echo %%i
Echo ************************
psexec \\%%i –h –u domain\username) -p password "enter commands for PsExec here"

)
pause


Author’s Tip:  You can avoid the PsExec EULA (End User License Agreement) pop-up by adding the following “-accepteula” parameter in your script:

psexec \\%%i -accepteula –h –u domain\username) -p password "enter commands for PsExec here"

The contents of your text file containing computer names (.e.g., computers.txt) should be like the following:

computer1
computer2
computer3
computer4

computers_list


Facebook Comments