r/PowerShell • • 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,0

And 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?

0 Upvotes

26 comments sorted by

8

u/FrickingNinja Sep 17 '23

Why do you need a batch in the first place, can you just use ps?

0

u/TahPenguin Sep 17 '23

A batch file seems to be working best with the logitech software where I set actions to the custom keys on the keyboard. I am open to suggestions.

4

u/stewie410 Sep 17 '23

I'm unfamiliar with Logitech's software, but if you can specify a command to be run, then using something like this should work:

powershell.exe -Command "& C:\path\to\your\script.ps1" -NoLogo -NonInteractive -NoProfile

To be quite honest, if I need a custom keybinding action I'd typically just use AutoHotKey -- but that's not always an option, either.

0

u/TahPenguin Sep 17 '23

It's a logitech keyboard with extra keys labeled G1-5, that's why I am using their software.

I modified my batch file to what you suggested, but sadly it not only still opens a window, but the script also doesn't work anymore.

2

u/dnalloheoj Sep 17 '23

It's a logitech keyboard with extra keys labeled G1-5, that's why I am using their software.

Common workaround would be to use the Logitech software and bind those G1-5 keys to something like F13-18, and then use AHK to run scripts from the F13-18 keypress. That said, not certain it'll fix your issue if the problem occurs when you're just executing the script from Powershell itself.

I've got 3 Logitech keyboards and 2 mice so I'm all for using their software when it's capable of it, but I still find myself using AHK somewhat regularly.

Is it currently just a key bound to Launch Application->script.bat that calls script1.ps1 ?

1

u/TahPenguin Sep 17 '23 edited Sep 17 '23

I have one key bound to F24 but other than that the software worked fine so far, so I never looked for anything else.

Binding it to the .ps1 file had no effect, that's why I created the batch file and then it worked.

So it's Launch Application->function.bat

But yeah, using AHK wouldn't solve the issue as the vbs script doesn't work when launched manually.

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

u/BlackV Sep 17 '23

Recommend using cscript not wscript well calling from a command line

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

u/C0gn171v3D1550n4nc3 Sep 17 '23

What happens if you call the bat from vbs?

1

u/TahPenguin Sep 17 '23

No change, sadly.

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

u/TahPenguin Sep 17 '23

Will look into it, thank you!

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

u/illsk1lls Sep 17 '23

Try(in powershell):

cmd /c start /min “” “c:\path to executable\orcommand”

1

u/jborean93 Sep 17 '23

Make sure:

  • You are using wscript.exe to run the vbs file

  • You specify -WindowStyle Hidden as 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

u/[deleted] Sep 17 '23

[removed] — view removed comment

1

u/TahPenguin Sep 17 '23

Will look into it, thanks for the suggestion!