Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening PowerShell Script and hide Command Prompt, but not the GUI

I currently open my PowerShell script through a .bat file. The script has a GUI. I tried to put this in my script but it hid the GUI as well and also kept looping because I wanted my GUI to continuously loop:

powershell.exe -WindowStyle Hidden -file c:\script.ps1

How do we run a script without Command Promp, but also not hide the gui? And is that the same if we run the script using a .bat file? Thank you!

like image 908
DrixlRey Avatar asked Nov 15 '16 19:11

DrixlRey


People also ask

How do I run a PowerShell script without displaying Windows?

To totally remove window you can do one of two things: 1: Run in a different user's context such as admin account (won't display any windows to the logged on user). Or 2: Use a vbscript with objshell. run with a hidden window flag to launch cmd.exe /c powershel.exe -file c:\script.

How do I hide a PowerShell command?

ps1 you need to use start-process. However, start-process with -WindowStyle hidden prevents the GUI from showing up. Removing -WindowStyle shows a command window behind your GUI. However, if you start-process with cmd.exe /k, it does work.

How do I run a PowerShell script in the background?

To run commands in the background in the PowerShell, you need to use Background job cmdlets. Background job means running commands/job in the background without occupying the console.


1 Answers

If you want to hide the Console behind the GUI I've had success with the following native functions:

# .Net methods for hiding/showing the console in the background
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'

function Show-Console
{
    $consolePtr = [Console.Window]::GetConsoleWindow()

    # Hide = 0,
    # ShowNormal = 1,
    # ShowMinimized = 2,
    # ShowMaximized = 3,
    # Maximize = 3,
    # ShowNormalNoActivate = 4,
    # Show = 5,
    # Minimize = 6,
    # ShowMinNoActivate = 7,
    # ShowNoActivate = 8,
    # Restore = 9,
    # ShowDefault = 10,
    # ForceMinimized = 11

    [Console.Window]::ShowWindow($consolePtr, 4)
}

function Hide-Console
{
    $consolePtr = [Console.Window]::GetConsoleWindow()
    #0 hide
    [Console.Window]::ShowWindow($consolePtr, 0)
}

Once the above functions have been added to your Form, simply call the Hide-Console function in your Form_Load event:

$OnFormLoad = 
{
    Hide-Console
}

If you need to show the Console, perhaps for debugging, you can easily show the console again by calling the Show-Console function:

$OnButtonClick = 
{
    Show-Console
}

For more information on the numbers you can pass to ShowWindow you can check out the ShowWindow documentation on MSDN here

Update based on comment

Thanks for this code. I tried to use it in my script, but where exactly am I suppose to put Hide-Console? My form load looks like this $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog()

To hide the console with this code, all you need to do is call Hide-Console. Since you already have code in the Shown event ($objForm.Add_Shown) we can simply add another line to call the command:

Change this:

$objForm.Add_Shown({$objForm.Activate()})

To this:

$objForm.Add_Shown({
    $objForm.Activate()
    Hide-Console
})

When your form is Shown the console will be hidden (You can still call Show-Console if you want to see it later).

like image 95
Bluecakes Avatar answered Sep 18 '22 21:09

Bluecakes