Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for showconsole.ps

(view source code of showconsole.ps as plain text)

  1. # Based on code by Hansson0728 on StackOverflow.com:
  2. # https://stackoverflow.com/a/60005029
  3. function Show-Console
  4. {
  5. 	param (
  6. 		[Parameter( Mandatory = $false, Position = 0 )]
  7. 		# 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).
  8. 		# Use the -List parameter for a list of values accepted by the -State parameter, and their meanings.
  9. 		[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' )]
  10. 		$State,
  11. 		[Parameter( Position = 0 )]
  12. 		# Hides the console window.
  13. 		[switch]$Hide,
  14. 		[Parameter( Position = 0 )]
  15. 		# Shows a list of accepted values for the -State parameter.
  16. 		[switch]$List,
  17. 		[Parameter( Position = 0 )]
  18. 		# Minimizes the console window.
  19. 		[switch]$Minimize,
  20. 		[Parameter( Position = 0 )]
  21. 		# Restores the minimized console window to normal again.
  22. 		[switch]$Restore,
  23. 		[Parameter( Position = 0 )]
  24. 		# Makes the hidden console window visible again.
  25. 		[switch]$Show,
  26. 		[Parameter( Position = 0 )]
  27. 		# Shows help for the Show-Console command.
  28. 		[switch]$Help
  29. 	)
  30.  
  31. 	$WindowState = [ordered]@{
  32. 		Hide = 0;
  33. 		ShowNormal = 1;
  34. 		ShowMinimized = 2;
  35. 		ShowMaximized = 3;
  36. 		Maximize = 3;
  37. 		ShowNormalNoActivate = 4;
  38. 		Show = 5;
  39. 		Minimize = 6;
  40. 		ShowMinNoActivate = 7;
  41. 		ShowNoActivate = 8;
  42. 		Restore = 9;
  43. 		ShowDefault = 10;
  44. 		ForceMinimized = 11
  45. 	}
  46.  
  47. 	$newstate = -1
  48.  
  49. 	if ( $Hide ) {
  50. 		$newstate = 0
  51. 	} elseif ( $List ) {
  52. 		"`nAccepted values for -State parameter, may be string or numeric:`n"
  53. 		"{0,-22}{1}" -f "String", "Numeric"
  54. 		"{0,-22}{1}" -f "======", "======="
  55. 		$WindowState.Keys | ForEach-Object {
  56. 			"{0,-22}{1,7}" -f $_, $WindowState[$_]
  57. 		}
  58. 		""
  59. 	} elseif ( $Minimize ) {
  60. 		$newstate = 6
  61. 	} elseif ( $Restore ) {
  62. 		$newstate = 9
  63. 	} elseif ( $Show ) {
  64. 		$newstate = 5
  65. 	} elseif ( $State ) {
  66. 		if ( -not ( [int]::TryParse( $State, [ref]$newstate ) ) ) {
  67. 			$newstate = $WindowState[$State]
  68. 		}
  69. 	} else {
  70. 		Get-Help Show-Console -Full
  71. 	}
  72.  
  73. 	if ( -not ( "Console.Window" -as [type] ) ) {
  74. 		Add-Type -Name Window -Namespace Console -MemberDefinition '
  75. 		[DllImport( "Kernel32.dll" )]
  76. 		public static extern IntPtr GetConsoleWindow( );
  77.  
  78. 		[DllImport( "user32.dll" )]
  79. 		public static extern bool ShowWindow( IntPtr hWnd, Int32 nCmdShow );
  80. 		'
  81. 	}
  82.  
  83. 	$consolePtr = [Console.Window]::GetConsoleWindow( )
  84. 	[void][Console.Window]::ShowWindow( $consolePtr, $newstate )
  85.  
  86. 	<#
  87. 	.SYNOPSIS
  88.  
  89. 	Manipulates the console window state
  90.  
  91. 	.DESCRIPTION
  92.  
  93. 	Use this command to manipulate the console window state, i.e. hide the console, minimize it, activate it, etc.
  94.  
  95. 	.INPUTS
  96.  
  97. 	None. You cannot pipe objects to Show-Console.
  98.  
  99. 	.OUTPUTS
  100.  
  101. 	None.
  102.  
  103. 	.EXAMPLE
  104.  
  105. 	PS> Show-Console -State Hide
  106. 	hides the console
  107.  
  108. 	.EXAMPLE
  109.  
  110. 	PS> Show-Console -State 0
  111. 	hides the console
  112.  
  113. 	.EXAMPLE
  114.  
  115. 	PS> Show-Console -Hide
  116. 	hides the console
  117.  
  118. 	.EXAMPLE
  119.  
  120. 	PS> Show-Console -State Show
  121. 	makes the hidden console visible again
  122.  
  123. 	.EXAMPLE
  124.  
  125. 	PS> Show-Console -State 5
  126. 	makes the hidden console visible again
  127.  
  128. 	.EXAMPLE
  129.  
  130. 	PS> Show-Console -Show
  131. 	makes the hidden console visible again
  132.  
  133. 	.EXAMPLE
  134.  
  135. 	PS> Show-Console -State Minimize
  136. 	minimizes the console window
  137.  
  138. 	.EXAMPLE
  139.  
  140. 	PS> Show-Console -Restore
  141. 	restores the minimized console window to normal again
  142.  
  143. 	.EXAMPLE
  144.  
  145. 	PS> Show-Console -List
  146. 	lists all accepted values for the -State parameter
  147.  
  148. 	.LINK
  149.  
  150. 	http://www.robvanderwoude.com/powershellexamples.php#Show-Console
  151.  
  152. 	.LINK
  153.  
  154. 	https://stackoverflow.com/a/60005029
  155. 	#>
  156. }

page last modified: 2024-04-16; loaded in 0.0071 seconds