Windows 7 psexec one liners

Multiple commands on one line
Simplest way for simple commands

This will run locally - opens a new shell, echo a b c and pause.

For more advanced things later on you really need to enclose in braces


 * Note, & will always run the next command (^& is escaped &), using && will only run if previous exists ok (would write escaped as ^&^&, in above).

Multiple commands split over multiple lines
This will run locally - opens a new shell, echo a b c and pause. (To use remotely add \\hostname and remove the & pause ^ line.)


 * Note, the ^ escapes the end of the line and the next character, so it is the same as the above example (becomes just ^&)

Multiple commands, lines, escapes
Correctly grant Everyone Modify access to a folder on a remote machine with hostname "mymachine", in this case used inside a batch file.

Single line FOR loop
Batch FOR loop with psexec, this is remotely registering 32bit .OCX files in 64bit Windows 7's syswow64 folder for backwards compatability with old apps.
 * Note escaped brackets
 * Note double percentage %%, not single % (as batch)

Run commands after psexec communication terminates
This runs detached (dont wait for process to terminates, -d) because otherwise communication will end when firewall is turned off and psexec exits (Win 7 behaviour), stopping firewall being able to be restarted.

gotchas
In a IF THEN statement you can use delayed expansion to get around variables with parenthesis in.

(This matters if say you had reference to a path variable with c:\Program Files (x86) in it)

Installing things under a SYSTEM account eg via SCCM
Sample batch file,
 * Note using "psexec -s" to emulate a SYSTEM account install on local machine, this would be removed on a live one.


 * Note
 * 1) running from a UNC, %~dp0 will be the path. (Dont use a mapped drive as the system account wont have it mapped)
 * 2) command processor shell (cmd /c) needed to run non 'core' programs.
 * 3) the escaped ^%%temp^%% variable is the SYSTEM's account temp folder. (It would be ^%temp^% if running directly on command line rather than batch)
 * 4) patch needs files copying to a local location to run (hence copy and push). IF NOT you will get a "One of the files has an invalid certificate, File: c:\Windows\system32\BBPatch.exe Invalid or no signature" (1602 error)

Remotely add a user to Win7
Sample section from a .bat adding an administrator account to a local machine with password xxxxx and setting it to never expire


 * Note %%localadmin1%% would be %localadmin1% on command line

Remotely installing Office 2010 language packs
This was quite a fun little challenge, I made a little .bat menu system to pick the office 2010 language, then it copied and ran it remotely.

1)copy file 2)extract pack 3)create the xml 4)setup the language pack.

main run loop


 * Note, the triple ^'d escapes on an enclosed echo to escape the left and right angle brackets (<>'s) from the xml and single escaped redirect angle bracket.
 * Note the SPACE prefix on the line after the "cmd /c (^" line, i.e. " c:\temp" . I don't understand why this was needed but you get errocode 1 if not. If anyone knows post a comment please!

DOS OneLiner
Strip a or b or c characters from string, e.g to strip 101b to just be 101, for a GEQ LEQ numerical evaluation. Note white spacing between each.

=Comments=