Show-PowerShell / Hide-PowerShell

Show-PowerShell / Hide-PowerShell

  • Comments 6

During the Week of WPF, someone requested an example of how to minimize the PowerShell window.

Here's a quick module to make it happen.  Copy/paste the code below into Documents\WindowsPowerShell\Packages\PowerShell\PowerShell.psm1

$script:showWindowAsync = Add-Type –memberDefinition @”
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
“@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru

function Show-PowerShell() {
     $null = $showWindowAsync::ShowWindowAsync((Get-Process –id $pid).MainWindowHandle, 10)
}

function Hide-PowerShell() {
    $null = $showWindowAsync::ShowWindowAsync((Get-Process –id $pid).MainWindowHandle, 2)
}

Now you can use the code below to Show and Hide PowerShell:

Add-Module PowerShell
# Minimize PowerShell
Hide-PowerShell
sleep 2
# Then Restore it
Show-PowerShell

Hope this Helps,
James Brundage[MSFT]

Leave a Comment
  • Please add 8 and 1 and type the answer here:
  • Post
  • Yes, It works but only when you launch the shortcut to Windows PowerShell V2 (CTP2) previously. If you first type cmd.exe and then you type:

    powershell.exe script.ps1 you have 2 different pids (one for cmd.exe and one for powershell.exe)

    It would be great that we can hide completely the powershell console window (not minimize) while the script is executing :)

  • I know this isn't the WPF way of doing things, but I wanted this functionality early on with Powershell v1 after having been spoiled by Quake-style terminals like Yakuake. I stumbled across AutoHotkey (http://www.autohotkey.com) which basically allows you to create mini scripts and map them to user-specified key combinations.

    The following AutoHotkey script shows/hides a console with a WindowsKey+` combination, although you can tweak it to whatever combination you want. If a console is not already running when the key combination is struck, a new Powershell window is launched:

    #`::

    DetectHiddenWindows, on

    IfWinExist ahk_class ConsoleWindowClass

    {

    IfWinActive ahk_class ConsoleWindowClass

     {

    WinHide ahk_class ConsoleWindowClass

    WinActivate ahk_class Shell_TrayWnd

    }

    else

     {

       WinShow ahk_class ConsoleWindowClass

       WinActivate ahk_class ConsoleWindowClass

     }

    }

    else

    Run C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    DetectHiddenWindows, off

    return

    Works a treat!

  • Check this out maybe:

    http://blog.sapien.com/index.php/2006/12/26/more-fun-with-scheduled-powershell/

    Uses VBScript to hide the PowerShell window.

  • Thanks for the info. Interesting workaround when we need to run powershell commands in background but it doesn´t work when we need to show windows forms :(

  • Thanks for the AutoHotkey script. It works great!

  • It also work with windows forms but you need launcher

    http://filesystemobject.blogspot.com/2009/04/pscript-der-powershell-script-launcher.html

Page 1 of 1 (6 items)