Skip to content

Instantly share code, notes, and snippets.

@jamiephan
Last active September 20, 2024 00:12
Show Gist options
  • Save jamiephan/b5671f3507acaaadd364fa45ea5c14ed to your computer and use it in GitHub Desktop.
Save jamiephan/b5671f3507acaaadd364fa45ea5c14ed to your computer and use it in GitHub Desktop.
Launch a batch file without showing the window, within one file and without third-party tools
@echo off
if not "%1"=="Opened_Via_PS" (
pushd %~dp0
REM //TODO: Use Env variable instead of passing checking arg.
rem Adopted from https://superuser.com/questions/62525/run-a-batch-file-in-a-completely-hidden-way
rem The " ^" at the end is similar to " \" for multi-line scripts.
rem Extend the %1 %2 (%3...) below if you have multiple command line args.
powershell.exe -NoLogo -NoProfile -WindowStyle Hidden -NonInteractive -ExecutionPolicy Bypass -Command ^
"(New-Object -ComObject WScript.Shell).Run('\"%cd%\%~n0%~x0\" Opened_Via_PS %1 %2', 0, 'false')"
popd
exit
)
rem %1 is used for "Opened_Via_PS" which check whether its opened via powershell above
set "arg1=%2"
set "arg2=%3"
rem ...and so on
rem Here you can put your stuff that can be hidden if its a commandline only tool.
echo "Normally you will see me, but now you cannot!"
notepad.exe %arg1%
msg * You closed the notepad.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment