1

I want to make a GUI for my Powershell scripts so others can easily use them too. I have a main-menu script witch calls some other scripts. For one of them I needed an elevated Powershell process.

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSFilePath`"" -Verb RunAs; exit }

Now my problem is, that not only the GUI from $PSFilePath is shown but also an empty console window in the background

I tried to use -WindowStyle Hidden

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSFilePath`"" -WindowStyle Hidden -Verb RunAs; exit }

But that resulted in both the console and the GUI being hidden.

Anyway to hide that console window but not the GUI?

bad
  • 25
  • 1
  • 5

2 Answers2

1

Try...

# Hide PowerShell Console
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);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)

But your post is a potential duplicate of this..

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

postanote
  • 15,138
  • 2
  • 14
  • 25
0

I found a solution some time ago, It's buried somewhere here, on StackOverflow, but can't find a link to it right now. It will open and immediately close empty console window.

Just paste it at the beginning of your script:

$dllvar = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $dllvar -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
nemze
  • 111
  • 4