Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a batch file to run a ps1 file but hide the command prompt after start?

I build a small tool with powershell and i open it through a batch-file. Batch file has the following content :

powershell -file "D:\scripts\Powershell\xxx.ps1"

Now it opens my tool but always displays the command prompt in background. I want the CMD to be hidden after running my file.

How to achieve that? I tried -hidden but then my programm is hidden :D

Thanks

like image 239
RayofCommand Avatar asked Mar 21 '23 14:03

RayofCommand


1 Answers

Try using the START utility in your batch file, it will launch the program in a new window and allow the batch's cmd window to exit in the background.

START powershell -file "D:\scripts\Powershell\xxx.ps1"

Be aware START exhibits [potentially] unexpected behavior when the first parameter contains double quotes.

If you're using GUI elements, or external applications, and you want to hide Powershell's window, but not the GUI, try the following:

START powershell -WindowStyle Hidden "D:\scripts\Powershell\xxx.ps1"

Here's an example of how I've used batch in tandem with PowerShell GUI:

Contents of C:\call.cmd:

START "" powershell -NoLogo -WindowStyle Hidden "C:\gui.ps1"

Contents of C:\gui.ps1:

# VB Assembly GUI example
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox(
    "Do you like VB in PowerShell?",
    [Microsoft.VisualBasic.MsgBoxStyle]::YesNo,
    "Hello"
)

# Windows Forms GUI example
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show(
    "Do you like Windows Forms?",
    "Hello",
    [System.Windows.Forms.MessageBoxButtons]::YesNo
)

# External app GUI example
& notepad.exe

Running the call.cmd batch will use START to launch PowerShell [exiting cmd] and PowerShell will hide its window, but otherwise display the GUI elements executed from the PS1 file.

like image 186
jscott Avatar answered Mar 24 '23 05:03

jscott