# Based on code by Hansson0728 on StackOverflow.com: # https://stackoverflow.com/a/60005029 function Show-Console { param ( [Parameter( Mandatory = $false, Position = 0 )] # Specifies the requested window state, either numeric (0..11) or by name (Hide, ShowNormal, ShowMinimized, ShowMaximized, Maximize, ShowNormalNoActivate, Show, Minimize, ShowMinNoActivate, ShowNoActivate, Restore, ShowDefault, ForceMinimized). # Use the -List parameter for a list of values accepted by the -State parameter, and their meanings. [ValidateSet( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'Hide', 'ShowNormal', 'ShowMinimized', 'ShowMaximized', 'Maximize', 'ShowNormalNoActivate', 'Show', 'Minimize', 'ShowMinNoActivate', 'ShowNoActivate', 'Restore', 'ShowDefault', 'ForceMinimized' )] $State, [Parameter( Position = 0 )] # Hides the console window. [switch]$Hide, [Parameter( Position = 0 )] # Shows a list of accepted values for the -State parameter. [switch]$List, [Parameter( Position = 0 )] # Minimizes the console window. [switch]$Minimize, [Parameter( Position = 0 )] # Restores the minimized console window to normal again. [switch]$Restore, [Parameter( Position = 0 )] # Makes the hidden console window visible again. [switch]$Show, [Parameter( Position = 0 )] # Shows help for the Show-Console command. [switch]$Help ) $WindowState = [ordered]@{ 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 } $newstate = -1 if ( $Hide ) { $newstate = 0 } elseif ( $List ) { "`nAccepted values for -State parameter, may be string or numeric:`n" "{0,-22}{1}" -f "String", "Numeric" "{0,-22}{1}" -f "======", "=======" $WindowState.Keys | ForEach-Object { "{0,-22}{1,7}" -f $_, $WindowState[$_] } "" } elseif ( $Minimize ) { $newstate = 6 } elseif ( $Restore ) { $newstate = 9 } elseif ( $Show ) { $newstate = 5 } elseif ( $State ) { if ( -not ( [int]::TryParse( $State, [ref]$newstate ) ) ) { $newstate = $WindowState[$State] } } else { Get-Help Show-Console -Full } 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 ); ' } $consolePtr = [Console.Window]::GetConsoleWindow( ) [void][Console.Window]::ShowWindow( $consolePtr, $newstate ) <# .SYNOPSIS Manipulates the console window state .DESCRIPTION Use this command to manipulate the console window state, i.e. hide the console, minimize it, activate it, etc. .INPUTS None. You cannot pipe objects to Show-Console. .OUTPUTS None. .EXAMPLE PS> Show-Console -State Hide hides the console .EXAMPLE PS> Show-Console -State 0 hides the console .EXAMPLE PS> Show-Console -Hide hides the console .EXAMPLE PS> Show-Console -State Show makes the hidden console visible again .EXAMPLE PS> Show-Console -State 5 makes the hidden console visible again .EXAMPLE PS> Show-Console -Show makes the hidden console visible again .EXAMPLE PS> Show-Console -State Minimize minimizes the console window .EXAMPLE PS> Show-Console -Restore restores the minimized console window to normal again .EXAMPLE PS> Show-Console -List lists all accepted values for the -State parameter .LINK http://www.robvanderwoude.com/powershellexamples.php#Show-Console .LINK https://stackoverflow.com/a/60005029 #> }