Skip to main content

How to Run a Command on Multiple Remote Computers in Batch Script

Managing a fleet of computers often requires running the same command on many machines simultaneously. Whether you need to update a registry key, install a software patch, or simply restart a service, doing this manually on each machine is inefficient.

In this guide, we will explore how to execute commands on multiple remote computers using Batch Script. We will focus on two primary methods: using the built-in WMIC tool and the industry-standard PsExec utility from Microsoft Sysinternals.

PsExec is a powerful command-line tool that lets you execute processes on remote systems. It is part of the Sysinternals Suite and is widely considered the best tool for this job because it captures the output of the remote command and displays it on your local console.

Prerequisites

  • Download PsTools.
  • Add the folder containing PsExec.exe to your system PATH (or run it from that directory).
  • Ensure "File and Printer Sharing" is enabled on target machines.
  • You must have administrative privileges on the remote computers.

Running on a Single Remote Machine

psexec -accepteula \\RemotePC cmd /c "echo Hello from Remote!"

Running on Multiple Computers from a Text File

Create a file named computers.txt containing the list of target machines:

Server01
Workstation05
192.168.1.10

Then, use the @ symbol to tell PsExec to read from this file.

Syntax:

psexec -accepteula @computers.txt -d cmd /c "gpupdate /force"
  • -accepteula: Automatically accepts the Sysinternals EULA, preventing PsExec from hanging on a license prompt during first run.
  • @computers.txt: path to the file containing computer names.
  • -d: (Optional) "Don't wait". The command will launch the process remotely and return control to your script immediately. Remove this if you want to see the output of each command sequentially.
  • cmd /c "...": This runs the command inside the remote command shell.

Example Script:

@echo off

if not exist "C:\Scripts\list.txt" (
echo list.txt not found at C:\Scripts\list.txt
pause
exit /b
)

echo Starting update on all computers...

:: Run ipconfig on all computers listed in list.txt
psexec -accepteula @C:\Scripts\list.txt ipconfig /flushdns

echo Done.
pause

Method 2: Using WMIC (Built-in)

If you cannot download third-party tools like PsExec, you can use WMIC (Windows Management Instrumentation Command-line), which is built into Windows. Note that WMIC is being deprecated in newer Windows 11 versions, but it is still widely available.

WMIC is more complex and less flexible than PsExec for interactive commands, but effective for starting processes.

Syntax:

wmic /node:@computers.txt process call create "cmd.exe /c echo Hello > C:\temp\test.txt"
  • /node:@computers.txt: Reads the list of computers from the file.
  • process call create "...": Creates a new process on the remote machine.
warning

Limitation: WMIC does not show you the output of the command. It only tells you if the process started successfully (ReturnValue = 0). You won't see "Hello" on your screen; it happens silently on the remote PC.

Method 3: Using a For Loop

If you don't want to use PsExec's @file feature or want more control (like pinging the machine before trying to connect), you can wrap the command in a for loop.

@echo off
setlocal

set "CommandToRun=gpupdate /force"

if not exist "computers.txt" (
echo computers.txt not found!
pause
exit /b
)

for /f "usebackq tokens=*" %%A in ("computers.txt") do (
echo Connecting to %%A...

REM Check if online first
ping -n 1 -w 500 %%A >nul 2>nul
if errorlevel 1 (
echo [OFFLINE] %%A - Skipping.
) else (
echo [ONLINE] %%A - Running command...
psexec -accepteula \\%%A cmd /c "%CommandToRun%"
)
echo ------------------------------------------
)

pause

Common Issues

"Access is Denied"

This is the most common error.

  • Ensure you are running the script as a Domain Admin or a user with local admin rights on the target.
  • If using a local account (not domain), you might need to add -u User -p Password to the PsExec command (insecure, not recommended for scripts).
  • Check that the admin$ share is accessible on the target (net use \\Target\admin$).

"The network path was not found"

  • The computer is offline.
  • Firewall is blocking port 445 (SMB).
  • "File and Printer Sharing" is disabled.

Summary

For running commands on multiple remote computers:

  1. PsExec is the best tool for most scenarios due to its ability to show remote output and handle credentials.
  2. WMIC is a built-in alternative for starting background processes silently.
  3. Combining these tools with a FOR loop allows you to add logic like connectivity checks (ping) before execution.