PsExec is a great tool, but commands become so long when combined with PsExec! Creating a batch script to essential alias PsExec makes it much more useable.

If I want to preform a simple show command on a remote PC with PsExec I need this much code:

psexec64 -accepteula \\HOSTNAME cmd /c "ipconfig"

That’s a lot to type and if I want to do anything more than a one word command it takes even more text. If I create a batch file to use with PsExec instead I can do the same thing like this:

ps.bat HOSTNAME ipconfig

Now all I need is the remote PC’s name and the command I want to run on that PC. Much better. You can also make the same script work with paramters, as in the above example, or interactively.

PsExec

Here is the whole script:

@echo off
:PARAMCHK
if "%~2"=="" goto :ASK
psexec64 -accepteula \\%1 cmd /c %2
goto :DONE
:START
psexec64 -accepteula \\%host% cmd /c %cmd% 2>nul
goto :DONE
:ASK
set host=127.0.0.1
set cmd=ipconfig
set /P host=Enter hostname or ip address[127.0.0.1]? 
set /P cmd=Enter cmd to excute on remote host["ipconfig"]? 
goto :START
:DONE

ps.bat

Just put the PsExec.exe file in your system path or refererence the complete path in the batch file. You can do the same thing with other pstools.

PsLoggedOn for example is useful to see if a remote computer is in use. Personally, I prefer the output of the “quser” command. A slight change to the ps.bat script can be affective to check the logged in user with “quser.”

@echo off
:PARAMCHK
if "%~1"=="" goto :ASK
psexec64 -accepteula \\%1 cmd /c "quser"
goto :DONE
:START
psexec64 -accepteula \\%host% cmd /c "quser"
goto :DONE
:ASK
set host=127.0.0.1
set /P host=Enter hostname or ip address[127.0.0.1]?
goto :START
:DONE

psl.bat

This script works the same as before, but only needs one parameter: hostname. Now, say you have a classroom full of PCs and you may need to check for logged on users before doing system maintenance.

@echo off
FOR /L %%G IN (1,1,40) DO (
	hostname
	PsExec -accepteula \\COMPUTERNAME%%G cmd /c "quser"
)
pause

psl_classroom.bat

This will do the same thing for a set of PCs with sequental hostnames!

Further reading:
https://docs.microsoft.com/en-us/sysinternals/downloads/PsExec

https://ss64.com/nt/psexec.html