How to avoid closing cmd after running command with win+r? - cmd

I press Windows+R to open run dialog box.
Then I write command ipconfig /all and press Enter.
cmd is opened with some network content but it closes immediately.
I want cmd not to close at all without my consent.
I googled that I can run two commands in windows run dialog when I separate them with & or &&. I also found out that pause or set /p= commands would stop cmd from closing. However, ipconfig /all && pause (and all 3 other combinations) fail - cmd still closes and the rest of the command is passed to ipconfig which "returns an error".

Quote Microsoft:
A console is closed when the last process attached to it terminates or calls FreeConsole.
In other words, Win32 console window will always be closed when the last program running inside it exits, and you cannot change this.
Solutions:
call cmd.exe /k first.
example:
cmd.exe /k ipconfig /all
With above trick, we call cmd.exe to execute ipconfig /all. Since cmd.exe not terminated, so the console will not closed.

Related

cmd /C command won't execute powershell script in the background

I tried running the command:
start /B cmd /C " powershell "Invoke-WebRequest -Uri "link here" -OutFile "%USERPROFILE%\Contacts\file.exe""&&start %USERPROFILE%\Contacts\file.exe"&& exit
But for some reason it executes the powershell script in the same window. So when I run this command it show me download bytes and after that process is finished and the file is opened then cmd can close. But I want to run that whole script in the background so I just open cmd run this command and it immediately closes and runs the command in the background.
To build on the helpful comments and your own answer, using a simplified command that waits 2 seconds and then launches Notepad:
powershell -WindowStyle Hidden "Start-Sleep 2; Start-Process Notepad.exe" & exit
Given that you want to close the current window and launch the PowerShell command invisibly, there is no need for using cmd.exe's internal start command.
As an aside:
As pointed out, start /B by design launches the given executable in the current window.
If you were to omit /B but still use start, powershell.exe would invariably launch visibly first, and -WindowStyle Hidden would only take effect afterwards, resulting in a brief flashing of the window - this is a long-standing problem that may get fixed in a future edition of PowerShell (Core), the modern, cross-platform successor to Windows PowerShell - see GitHub issue #3028.
The above invokes powershell.exe synchronously, which means that it will run in the current cmd.exe window, but - due to -WindowStyle Hidden - then hides the current window.
That is, the current window is virtually instantly hidden, but the cmd.exe session lives on; once the powershell.exe call has completed, however, & exit ensures that the cmd.exe session too is exited.

How to keep console window open even after program started from batch-file has been terminated?

I need to create a batch-file (.bat) which keeps the console window open even after the started program has terminated (for the purpose of examining the console window output). For that I tried to use the "/WAIT" switch:
#ECHO OFF
title test
#ECHO ON
start "" /WAIT "C:\My CLI Tools\7zip\x64\7za.exe"
But despite using the "/WAIT" switch the console window automatically closes after 7za.exe is terminated. (Obviously, the "/WAIT" switch waits only until the started program has been terminated).
So how can I make the console window stay open even after 7za.exe has been terminated?
start /wait tells batch to wait for the program to terminate, before performing next line, or to finish script. to keep it open and do nothing you can use pause:
#ECHO OFF
title test
#ECHO ON
"C:\My CLI Tools\7zip\x64\7za.exe"
pause
Or timeout:
#ECHO OFF
title test
#ECHO ON
start "" /WAIT "C:\My CLI Tools\7zip\x64\7za.exe"
timeout /t 300
But both these will do nothing until it either times out, or you press any key.
If you were expecting output in cmd window, and the command actually provides output to cmd console, then you should not start it outside of the current console window. Just do:
#ECHO OFF
title test
#ECHO ON
"C:\My CLI Tools\7zip\x64\7za.exe"
pause

Windows CMD - How to pass two commands in Start

I want to open a new command prompt and ping on that one and then pause the new screen so that it does not go away.
I used the following two command and it is not behaving as I expected.
start "mytitle" ping google.com pause
start "mytitle" ping google.com & pause
The started screen goes away and the pause command in run in the first prompt.
What am I missing?
On using the command line
start "mytitle" ping google.com & pause
the Windows command interpreter reads it like writing:
start "mytitle" ping google.com
pause
So first START is executed to run ping in a separate command process and while ping is running parallel the current command process continues with waiting for an input from handle STDIN. The ampersand on command line not found within a double quoted argument string is interpreted as additional command to execute after executing START in current command process.
You have to use:
start "mytitle" cmd /C "ping google.com & pause"
Or with full qualified file names:
start "mytitle" %SystemRoot%\System32\cmd.exe /C "%SystemRoot%\System32\ping.exe google.com & pause"
The internal command START of cmd.exe is designed for running one application. It is not designed to run a command line with one or more commands like Windows command interpreter itself.
Now a separate command process is started with cmd.exe which should close automatically because of option /C after finishing the execution of the command line specified in double quotes as second argument for started cmd.exe containing the instructions to execute ping.exe and then pause.

Hide CMD after opening

Does anybody know how to hide a console window (cmd), by just typing something into the console (not usbing VBS, batch-file or whatever), just using the cmd?
Is there a way to minimize it but still be able to type something?
Also heard of a possibility to just move the window out of your monitors size, does this work and if yes how?
You can start it minimized with start /min or start a new cmd minimized and close the old cmd start /min && exit but you loose the focus, you can't type commands in the minimized cmd, you need to klick it and bring it to front. You could execute a bat file with your commands (to execute them "invisible") start /min cmd.exe /C mybat.bat && exit
For "windows+r" (run) you can use: cmd.exe /C "start /min cmd"

Windows 8.1 unable to run batch file without command prompt flashing

I have scheduled a task to run a batch file, and currently every time the task executes, there is a command prompt box that flashes. This is very undesirable. The node.js command prompt window executes minimized correctly, it is the windows command prompt that flashes.
Here is the action that the scheduled task runs:
cmd /min /c "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat"
and here is the contents of the batch file, (probably unnecessary to show, but I was originally trying to run these commands straight from the task scheduler. Putting them in this batch file instead has got me closest to what I actually want to happen: run a javascript file with node.js minimized):
cd "C:\Program Files (x86)\BuildingIntelligence\javadobe\" & START /MIN node index.js /exit
A list of the commands I've tried:
cmd.exe /c start /min "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat"
cmd.exe /c start /min "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat" & /exit
%comspec% /c start "" /min "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat"
all of these commands leave the command prompt box open, not closing it but minimizing it after it opens. There is also a notification that says "not enough storage is available to process this command".
I need to run this task with the highest privileges, and while it needs to be invisible most of the time, it needs to be able to show a dialog box occasionally when the task is run.
Thanks in advance for any help!
Basically, if what you have tried doesn't work, you're going to need a third party tool. NirCMD should do it and it's freeware.
NirCMD exec hide "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat"
At the bottom of this page there is a link for the download. This page covers what the tool is capable of.

Resources