Welcome to MSDN Blogs Sign in | Join | Help

Show-PowerShell / Hide-PowerShell

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]

Published Tuesday, June 03, 2008 1:29 AM by PowerShellTeam
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Show-PowerShell / Hide-PowerShell

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 :)

Tuesday, June 03, 2008 11:33 AM by Pablo

# re: Show-PowerShell / Hide-PowerShell

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!

Wednesday, June 04, 2008 4:20 PM by Just Another Sysadmin

# @Pablo

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.

Friday, June 06, 2008 1:57 PM by Marco Shaw

# re: Show-PowerShell / Hide-PowerShell

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 :(

Monday, June 09, 2008 3:41 AM by Marco Shaw

# @Just Another Sysadmin

Thanks for the AutoHotkey script. It works great!

Wednesday, August 06, 2008 8:44 AM by Pepa Stefan

# re: Show-PowerShell / Hide-PowerShell

Friday, July 17, 2009 6:32 PM by Boris

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker