r/PowerShell • u/TahPenguin • Sep 17 '23
Question run .bat w/o a cmd window flashing
Hi,
I wrote a simple ps1 script and use a hotkey to run a .bat file that executes it.
Everything works fine, but: I'd like it to execute it in the background and not show a cmd window.
I researched and tried:
command = "powershell.exe -nologo -command C:/PS1-FILE-PATH"
set shell = CreateObject("WScript.Shell")
shell.Run command,0And saved it as a .vbs script.
There aren't any windows popping up anymore, but the script doesn't work either.
I also tried -windowstyle hidden etc. but to no avail.
EDIT: I am curious as to why this got downvoted? Why is it frowned upon to ask questions?
3
u/C0gn171v3D1550n4nc3 Sep 17 '23
Command = "powershell.exe -nologo -executionpolicy Unrestricted -file C:\script.ps1" Set shell = createObject("wscript.shell") Shell.run command,0
1
u/TahPenguin Sep 17 '23
Thanks for the suggestion. Sadly no cigar. Same behaviour - no window pops up and the script does not get executed.
4
2
u/C0gn171v3D1550n4nc3 Sep 17 '23
Maybe it's the script
1
u/TahPenguin Sep 17 '23
It's working when run from my batch file, so I'd rule that out.
1
1
u/TalkingToes Sep 17 '23
Can you make it start minimized?
1
u/TahPenguin Sep 17 '23
I googled a bit after reading your comment. How would I do that? Couldn't find anything like it.
1
u/TalkingToes Sep 17 '23
Create a shortcut to the .BAT file. Set it to minimize. Put in the Startup folder (or run via task scheduler).
1
u/ostekages Sep 17 '23
Perhaps a bit convoluted, but you can create a scheduled task, run it with a different user and then just run it as required or schedule it to be run when needed (e.g. On system startup).
The different user is the key not have it pop up
1
u/TahPenguin Sep 17 '23
Task Scheduler was my first thought but it wouldn't be practical. What I have created with the script is a toggle between the balanced and high performance mode for the CPU. Not every software runs well in balanced mode so I always toggled depending on the app.
It works, I just can't figure out how to hide the pop up window ^ ^
It's not a big deal, just a split second, but I thought it would be easier to hide.
1
u/ostekages Sep 17 '23
Well you could use schtasks run, then you just need a short command to execute the scheduled task.
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks-run
1
1
u/Thedguy Sep 17 '23
Have you tried psexec?
1
u/TahPenguin Sep 17 '23
No, I have not. Will have to read into it as it's the first time I hear about this! Thanks for the suggestion!
2
1
u/jborean93 Sep 17 '23
Make sure:
You are using
wscript.exeto run the vbs fileYou specify
-WindowStyle Hiddenas pwsh arguments to run with
Using wscript is important as that's a GUI application and won't spawn the console Windows whereas cscript will. This is important as you are trying to get ahead of starting a console application by specifying the flags to create it with a hidden window that your vbs script does.
I'm not sure if -WindowStyle Hidden is important on the pwsh args but it may be needed to ensure it doesn't then unhide the window when itself starts.
1
8
u/FrickingNinja Sep 17 '23
Why do you need a batch in the first place, can you just use ps?