I have the following bat file:
#echo off
start /min powershell "& 'C:\Admin\c.ps1'"
exit
I don't want to display the powershell window as this is an administrative script that runs on startup.
I was under the impression that using start /min will solve it, but It's still displaying.
PowrShell has a switch to launch itself hideen. That said, the console may be visible for a short period of time until it disappears. Since this is an administrative script, I would also recommend using the NonInteractive switch to prevent someone from interfering with it
#echo off
powershell -NonInteractive -File C:\Admin\c.ps1
exit
If you can't afford the console to be visible at all, consider using a vbs file to launch powershell:
Set oShell = CreateObject("WSCript.Shell")
oShell.Run("powershell -NonInteractive -File C:\Admin\c.ps1",0,False)
If you use 'Task Scheduler', 'Run Once On Startup', there is an option to hide windows like this.
START command has option /b to start an application without creating a new window.
try
start /b powershell "& 'C:\Admin\c.ps1'"
You can use a VBScript to stop the window from appearing at all.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("C:\Admin\c.ps1"), 0, True
Plus you can use this instead of the batch file, so there will be no cmd window either.
Hope this helps.
Related
I am troubleshooting a problem with a device keeping a Windows 10 PC awake using the powercfg -requests command. I have a BAT file containing the following so it can be run quickly via shortcut with run as admin properties:
cmd /k powercfg -requests
This works, but in addition to keeping the window open, I'd like to be able to keep sending powercfg -requests by hitting arrow up/enter when needed, as would normally happen if I opened a command prompt and typed it manually.
I've searched quite a bit here, and aside from making it repeat automatically,
I haven't found anything other than sending arrow keystrokes in a BAT file.
I did see something which implied it might be possible to print the command so it's entered at the prompt
after the command has executed, which would be fine too, but haven't had much luck.
Is this possible?
Sorry, there is not a simple way...
This works:
#if (#CodeSection == #Batch) #then
#echo off
start "" /B cmd /K
timeout /T 1
CScript //nologo //E:JScript "%~F0" "powercfg -requests{ENTER}"
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
For a further description on the method used, see this answer
Use PowerShell instead. You can right click on Desktop > New Shortcut and paste this then save
powershell -NoExit -Command "Add-Content -Path (Get-PSReadlineOption).HistorySavePath 'powercfg -requests'; Invoke-Expression 'powercfg -requests'"
It's better to create a new shortcut because you can set it to always run as admin although it's still possible to put it in a *.bat/*.ps1 file
You can also shorten it like this
powershell -NoE -C "$c = 'powercfg -requests'; ac (Get-PSReadlineOption).HistorySavePath $c; iex $c"
PowerShell is far more flexible and powerful than batch. It also has a persistent history so it's far more convenient to use PowerShell. You may not even need this and just press Ctrl+R to search through history for the powercfg command just like in bash/zsh
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.
I'm trying to write a command in a bat file to run an installer exe file. The important part is to start and run the installer in silent mode. To clarify, I DO NOT want the user to see the installer and click through the wizard. They should just be able to double click the bat file and walk away. I have attempted this command in my bat file:
#echo off
REM Next command runs installer in silent mode
start /d "%USERPROFILE%\Desktop" MyInstaller_7.1.51.14.exe –s –v –qn
The –s –v –qn are supposed to enable the installer to run in the background, but they are not working.
Can anyone help me improve my command in my bat file so that MyInstaller_7.1.51.14.exe is indeed running in the background, silently, with no UI or wizard of any kind visible to the user??
Please help.
You can try one of these START command options to see if it gives you the effect you want:
/B = Start application without creating a new window
/MIN = Start window minimized
Edited:
Try putting the command with its switches inside quotes:
start /d "%USERPROFILE%\Desktop" "MyInstaller_7.1.51.14.exe –s –v –qn"
Another solution you can test :
Create a file RunHide.vbs and put this line in it :
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
and then run your batch file like this :
wscript.exe "RunHide.vbs" "Install.bat"
and your batch file will be run without any windows (and maybe your Installer to)
I finally figured it out.
Here is the correct code:
#echo off
REM Next command runs installer in silent mode
start "%USERPROFILE%\Desktop" MyInstaller_7.1.51.14.exe /s /v /qn
The change was between –s –v –qn and /s /v /qn where the former does not work, and the latter does.
I am using the following command in the registry to run a vbscript locally from Windows Explorer's context menu:
cmd /T:1F /K "cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%1""
I am using the /K switch for 2 reasons: to keep the console window open in case of script errors, and to change the back and fore colors of the console (this is actually done with the /T switch, but for some reason that I don't understand, the colors revert to default when the /C switch is used.
My question: how can I close the host console window (attached to the "cmd.exe" process) on normal execution end? WScript.Quit obviously returns to the console prompt. I thought about a WMI query for Win32_Process based on CommandLine, but that doesn't work if the script is invoked multiple times with the same parameter (i.e. file). Is there a way to obtain the top most parent process's id for example, which I can then use to terminate?
I believe you can simply append &&exit to your command string:
cmd /T:1F /K "cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%1"&&exit"
Note that your quoted arguments are not quoted because of the extra enclosing quotes. You might be better off with:
cmd /T:1F /K ^"cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%~1"^&&exit^"
Update
The script engine does not set the exit code upon runtime errors - it leaves that task to the script writer. It does set a positive exit code if there is a syntax error. That is a very unfortunate design.
I think the easiest thing to do is to modify your VBS script to always exit with a negative value upon success: WScript.Quit -1. Then you can conditionally EXIT the console only if the exit code is negative.
cmd /T:1F /K ^"cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%~1"^||if not errorlevel 0 exit^"
I have this bat file and I want to minimize the cmd window when I run it:
#echo off
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb
I want the command window minimized immediately. Any ideas on how to do this?
There is a quite interesting way to execute script minimized by making him restart itself minimised. Here is the code to put in the beginning of your script:
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
... script logic here ...
exit
How it works
When the script is being executed IS_MINIMIZED is not defined (if not DEFINED IS_MINIMIZED) so:
IS_MINIMIZED is set to 1: set IS_MINIMIZED=1.
Script starts a copy of itself using start command && start "" /min "%~dpnx0" %* where:
"" - empty title for the window.
/min - switch to run minimized.
"%~dpnx0" - full path to your script.
%* - passing through all your script's parameters.
Then initial script finishes its work: && exit.
For the started copy of the script variable IS_MINIMIZED is set by the original script so it just skips the execution of the first line and goes directly to the script logic.
Remarks
You have to reserve some variable name to use it as a flag.
The script should be ended with exit, otherwise the cmd window wouldn't be closed after the script execution.
If your script doesn't accept arguments you could use argument as a flag instead of variable:
if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit
or shorter
if "%1" == "" start "" /min "%~f0" MY_FLAG && exit
Use the start command, with the /min switch to run minimized. For example:
start /min C:\Ruby192\bin\setrbvars.bat
Since you've specified a batch file as the argument, the command processor is run, passing the /k switch. This means that the window will remain on screen after the command has finished. You can alter that behavior by explicitly running cmd.exe yourself and passing the appropriate switches if necessary.
Alternatively, you can create a shortcut to the batch file (are PIF files still around), and then alter its properties so that it starts minimized.
The only way I know is by creating a Windows shortcut to the batch file and then changing its properties to run minimized by default.
Using PowerShell you can minimize from the same file without opening a new instance.
powershell -window minimized -command ""
Also -window hidden and -window normal is available to hide completely or restore.
source: https://stackoverflow.com/a/45061676/1178975
If you want to start the batch for Win-Run / autostart, I found I nice solution here https://www.computerhope.com/issues/ch000932.htm & https://superuser.com/questions/364799/how-to-run-the-command-prompt-minimized
cmd.exe /c start /min myfile.bat ^& exit
the cmd.exe is needed as start is no windows command that can be executed outside a batch
/c = exit after the start is finished
the ^& exit part ensures that the window closes even if the batch does not end with exit
However, the initial cmd is still not minimized.
One way to 'minimise' the cmd window is to reduce the size of the console using something like...
echo DO NOT CLOSE THIS WINDOW
MODE CON COLS=30 LINES=2
You can reduce the COLS to about 18 and the LINES to 1 if you wish.
The advantage is that it works under WinPE, 32-bit or 64-bit, and does not require any 3rd party utility.
If you type this text in your bat file:
start /min blah.exe
It will immediately minimize as soon as it opens the program. You will only see a brief flash of it and it will disappear.
You could try running a script as follows
var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c setrbvars.bat", WindowStyle_Hidden)
save the file as filename.js
Yet another free 3rd party tool that is capable of minimizing the console window at any time (not only when starting the script) is Tcl with the TWAPI extension:
echo package require twapi;twapi::minimize_window [twapi::get_console_window] | tclkitsh -
here tclkitsh.exe is in the PATH and is one of the tclkit-cli-*-twapi-*.exe files downloadable from sourceforge.net/projects/twapi/files/Tcl binaries/Tclkits with TWAPI/. I prefer it to the much lighter min.exe mentioned in Bernard Chen's answer because I use TWAPI for countless other purposes already.
You can minimize the command prompt on during the run but you'll need two additional scripts: windowMode and getCmdPid.bat:
#echo off
call getCmdPid
call windowMode -pid %errorlevel% -mode minimized
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb
One option is to find one of the various utilities that can change the window state of the currently running console window and make a call to it from within the batch script.
You can run it as the first thing in your batch script. Here are two such tools:
min.exe
http://www.paulsadowski.com/wsh/cmdprogs.htm
cmdow
http://www.commandline.co.uk/cmdow/index.html
Another option that works fine for me is to use ConEmu, see http://conemu.github.io/en/ConEmuArgs.html
"C:\Program Files\ConEmu\ConEmu64.exe" -min -run myfile.bat
try these
CONSOLESTATE /Min
or:
SETCONSOLE /minimize
or:
TITLE MinimizeMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "MinimizeMePlease"') DO CMDOW %%A /MIN
http://conemu.github.io/en/ConEmuArgs.html download flagged by Virus Total.
May have Malware.