Advertisement
keebz

Show/Hide PowerShell Window for GUI

Feb 26th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -Name Window -Namespace Console -MemberDefinition '
  2. [DllImport("Kernel32.dll")]
  3. public static extern IntPtr GetConsoleWindow();
  4.  
  5. [DllImport("user32.dll")]
  6. public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
  7. '
  8.  
  9. function Show-Console {
  10.    $consolePtr = [Console.Window]::GetConsoleWindow()
  11.   #5 show
  12.  [Console.Window]::ShowWindow($consolePtr, 5)
  13. }
  14.  
  15. function Hide-Console {
  16.     $consolePtr = [Console.Window]::GetConsoleWindow()
  17.   #0 hide
  18.  [Console.Window]::ShowWindow($consolePtr, 0)
  19. }
  20.  
  21. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  22. $Form = New-Object System.Windows.Forms.Form
  23.  
  24. $showButton = New-Object System.Windows.Forms.Button
  25. $showButton.Text = 'ShowConsole'
  26. $showButton.Top = 10
  27. $showButton.Left = 10
  28. $showButton.Width = 100
  29. $showButton.add_Click({Show-Console})
  30. $form.controls.Add($showButton)
  31.  
  32. $hideButton = New-Object System.Windows.Forms.Button
  33. $hideButton.Text = 'HideConsole'
  34. $hideButton.Top = 60
  35. $hideButton.Left = 10
  36. $hideButton.Width = 100
  37. $hideButton.add_Click({hide-Console})
  38. $form.controls.Add($hideButton)
  39.  
  40. $Form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement