Skip to content

Instantly share code, notes, and snippets.

@cjerrington
Created January 31, 2024 02:44
Show Gist options
  • Save cjerrington/32a8c788dded84f4f9fa85db8995f35a to your computer and use it in GitHub Desktop.
Save cjerrington/32a8c788dded84f4f9fa85db8995f35a to your computer and use it in GitHub Desktop.
Powershell function to show/hide the console window. I’ve used this format to start a process of a powershell GUI and hid the console after load. Can then be used to toggle the console as well.
function Show-Console
{
param ([Switch]$Show,[Switch]$Hide)
if (-not ("Console.Window" -as [type])) {
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);
'
}
if ($Show)
{
$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
$null = [Console.Window]::ShowWindow($consolePtr, 5)
}
if ($Hide)
{
$consolePtr = [Console.Window]::GetConsoleWindow()
#0 hide
$null = [Console.Window]::ShowWindow($consolePtr, 0)
}
}
# to hide the console
Show-Console -hide
# to show the console
Show-Console -show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment