4

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

0

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

hey nice to meet you again. Now i have an empty Powershell window opented + my program. Is there a way to have just my program ?
Does the xxx.ps1 script launch an external application, or create is's own GUI? Just trying to understand what exactly you require.
I've added an example adapted from some stuff I've used before. It should allow you to display GUI elements from a PS1, but hide the PowerShell window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.