1

I'm executing a command as another user on the same machine using PsExec from cmd like this:

PsExec.exe -u myuser -p mypass timeout 4

When I do so, a new cmd window opens containing the output of timeout 4, which stays open until the command completes. How do I prevent this new window from opening or showing?

The closest I got was with the following, but this is still unsatisfactory, as the new window is visible for a few moments before minimizing:

PsExec.exe -u myuser -p mypass cmd.exe /c "start /min timeout 4"

The only other question I found regarding this was starting from PowerShell, but I'm in the standard cmd. I'm on Windows 10, PsExec v2.2.

Michel
  • 117
  • 1
  • 12
  • So you are using a console program that will start other console programs in it's own console. Yet you don't want a console. Don't use console programs if you don't want one. This is merely a question of *why your solution to an unknown problem doesn't work.* You can always use 2 VBScripts to start both programs. – user1292580 May 19 '21 at 05:55
  • As stated in the question, I'm using `PsExec` to execute a command as another user on the same machine. This opens a new window, which I want to avoid. If your VBScript suggestion can solve this, please post it as an answer. – Michel May 19 '21 at 06:16
  • Apart from using wrong technology - *console programs* automatically get a console - that's why they are called console programs. You said *This does not work, the command window opened by PsExec (the one containing the output of timeout 4) will still show. Tested with both wscript.exe and cscript.exe.* So use the technique twice - once for PsExec (not part of windows - runas is but is also console program) and once for a pointless call to the console program `timeout`. As I said a unspecified problem. – user1292580 May 19 '21 at 06:29
  • 1
    I chose `timeout 4` as a [mwe](https://stackoverflow.com/help/minimal-reproducible-example) where the opened window is visible more than a second. Your suggestion got me on the right track, by putting only `timeout 4` in the script and calling `wscript` with `PsExec` did not open the additional window. Great! As you came up with it, do you want to post it as an answer, so that I can accept it? – Michel May 19 '21 at 06:43

4 Answers4

1

I would use the windows scripting host (old tech) to launch the window as hidden. I also use this method to do things through task scheduler that I don't want popping up.

This method will give you the flexibility to actually control the window creation flags (which you can not do without code).

  • You will need to register the JScript handler FROM AN ELEVATED COMMAND PROMPT first as it is no longer registered by default.

Like this: regsvr32 %systemroot%\system32\jscript.dll

Next, create a JScript text file with the following contents.


    var oShell = new ActiveXObject("WScript.Shell");
    //oShell.Popup("Test if the script is launching by removing this comment");
    oShell.Run( "PsExec.exe -u user_name -p user_pass timeout 4", 
                0 /* SW_HIDE */, 
                true /* bWaitOnReturn */
                );

Now run this JScript file using the command line:

wscript.exe <path_to_jscript_file> or cscript.exe <path_to_jscript_file>

Which one you use will depend on what behavior you are seeking. Play with each.

Señor CMasMas
  • 4,794
  • 1
  • 11
  • 27
  • This does not work, the command window opened by PsExec (the one containing the output of timeout 4) will still show. Tested with both `wscript.exe` and `cscript.exe`. – Michel May 17 '21 at 11:02
  • I am seeing different behavior on my box. I am playing with it in the debugger right now. – Señor CMasMas May 17 '21 at 13:07
  • [This solution](https://stackoverflow.com/questions/50990966/powershell-in-task-manager-shows-window/51007810#51007810) uses more or less the same idea that I had.. but people are saying that it works for them. They aren't using PSExec, but that part shouldn't matter. – Señor CMasMas May 17 '21 at 13:10
  • Thanks for looking into this again. I think it matters that I am using PsExec, because the window that I see is the window spawned by PsExec, not the window containing PsExec. To be clear, if I change the `SW_HIDE` in your code to 1 (`SW_NORMAL`), I will see two new windows: One containing the output of `timeout` (which I also see when using `SW_HIDE`) and one with `PsExec.exe` in its title (which I do not see when using `SW_HIDE`). – Michel May 18 '21 at 07:06
  • I don't have time right now.. but perhaps try combining psexec with start /b ? – Señor CMasMas May 18 '21 at 13:33
0
  • Make a shortcut to a batch file with text, PsExec.exe -u myuser -p mypass timeout 4.
  • Right-click on the shortcut and select Properties.
  • On the Shortcut tab, set Run: to Minimized.
DrMoishe Pippik
  • 25,661
  • 4
  • 36
  • 54
  • This does not work, the command window opened by PsExec (the one containing the output of `timeout 4`) will still open un-minimized. I tried both double-clicking the created shortcut and calling the `lnk` from a cmd window with the same results. – Michel May 17 '21 at 09:45
0

I've since found two solution myself. The simpler one is to use PAExec, which is a free, redistributable and open source equivalent to PsExec. It takes the same arguments and more, but doesn't open an extra window.

For the second solution, the comment by user1292580 got me on the right track together with the answer by Señor CMasMas. The catch was to put only the timeout command in the script file and then call that with PsExec. So, using this .js file:

var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("timeout 2", 0, true);

Or alternatively this .vbs file:

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("timeout 2", 0, true)

and then calling PsExec.exe wscript <the_script_file> did the trick.

Michel
  • 117
  • 1
  • 12
0

You can use -d parameter like:

PsExec.exe -d -u myuser -p mypass timeout 4
Destroy666
  • 5,299
  • 7
  • 16
  • 35
Dmitri
  • 1
  • 1