PowerShell Prompt Here PowerToy

Today, we have a guest post from Michael Murgolo. He's developed a PowerShell Prompt Here powertoy (like the one we all know and love,) but this one doesn't suffer from the uninstall bug that the current CMD Prompt Here powertoy (or its derivatives) do. And the explanation is awesome, to boot.

 

An Old PowerToy is New Again

by Michael Murgolo

PowerShell PowerToys Download

One of the most popular Windows PowerToys is the venerable Command (or CMD) Prompt Here.  This PowerToy is still available as part of the Microsoft PowerToys for Windows XP or the Windows Server 2003 Resource Kit Tools.  With this PowerToy installed, you can right click on a folder or drive in Windows Explorer and select CMD Prompt Here from the context menu.  This will open a command prompt with the selected folder as the current directory.

Because this is so handy, I use this PowerToy numerous times as day.  As I was trying to learn and use Windows PowerShell, I found myself wishing for the same functionality in PowerShell.  So I grabbed the Setup Information (INF) File for CMD Prompt Here, cmdhere.inf, from the Windows Server 2003 Resource Kit Tools and modified it to create a PowerShell Prompt Here context menu.  This INF file is included in the PowerShell PowerToys Download.  To install it, right click on the INF file and select Install.

In the course of creating the PowerShell version, I discovered that the original CMD Prompt Here PowerToy had a bug that would leave behind a dead context menu entry when it was uninstalled.  I have provided an updated version of cmdhere.inf for you convenience also in the PowerShell PowerToys Download. 

How it works

Both of these PowerToys take advantage of the fact that these context menu entries are configured in the Windows Registry under keys associated with the Directory and Drive object type.  This is done in the same way that context menu actions are associated with file types.

For example, when you right click on a .txt file in Explorer, you get several actions at the top of the list like Open, Print, and Edit.  To find out how these items are configured, let’s take a short journey through the HKEY_CLASSES_ROOT Registry hive.  If you open the Registry Editor and expand the HKEY_CLASSES_ROOT branch you will see keys that are named for file types like .doc, .txt, etc.  If you click on the .txt key, you will see the (Default) value is txtfile.

This is the object type associated with a .txt file.  If you scroll down and expand the txtfile key, then expand the shell key under that, you will see keys named for some of the context menu entries for a .txt file.  (You will not see all of them because there are other methods for creating context menus.)  Under each of these is a command key.  The (Default) value under the command key is the command line that Windows executes when you select that context menu item.

The PowerToys for both the CMD prompt and PowerShell prompt use this technique to configure the CMD Prompt Here and PowerShell Prompt Here context menu entries. There are no file types associated with drives and directories, but there is a Drive and a Directory key under HKEY_CLASSES_ROOT associated with those object types.

The PowerShellHere.ps1 script (in the attached file) shows how to set the same Registry entries as PowerShellHere.inf. This can be used in the case where an entry is Add/Remove Programs to uninstall the PowerToy is not desired.

To remove these entries you can use RemovePowerShellHere.ps1, also included in the PowerShell PowerToys Download.

  

 

Michael Murgolo is a Senior Infrastructure Consultant for Microsoft Consulting Services. He is focused in the areas of operating systems, deployment, network services, active directory, systems management, automation, and patch management.

4 Responses to “PowerShell Prompt Here PowerToy”

  1. A. Petitjean writes:

    It’s a very good trick, very useful ! But when I use PowerShell Prompt Here, the background of the console is black instead of blue (the original) and the size of the console is reduced too ?

  2. Lee writes:

    Hi —

    The reason is that the PowerShell launch customizations (colour, etc) are only available when you load Powershell through its .lnk file.

    If you customize the properties in the window that opens (right-click the options, pick "Properties,) then those customizations will stay for all PowerShell windows that open.

  3. Web-Crawling Stickler writes:

    Noticed a bug in the command, which doesn’t switch to the correct drive with CMD:
    [CmdHere.AddReg]

    HKCR,Directory\Shell\CmdHere\command,,,"%11%\cmd.exe /k cd ""%1"""

    HKCR,Drive\Shell\CmdHere\command,,,"%11%\cmd.exe /k cd ""%1"""

    When you right-click a folder on a drive other than C:, the command doesn’t seem to work, because though the folder was changed, the current drive was not.
    The corrected version should read
    [CmdHere.AddReg]

    HKCR,Directory\Shell\CmdHere\command,,,"%11%\cmd.exe /k cd /d ""%1"""

    HKCR,Drive\Shell\CmdHere\command,,,"%11%\cmd.exe /k cd /d ""%1"""
    Thanks for the associations!

  4. Michael Murgolo writes:

    This fix was done for the latest version of CmdHere.inf that is available in my Script Elevation PowerToys for Windows Vista (TechNet Magazine, June 2007). The download link on this page now points to the TechNet Magazine article, which has the latest download. Even though the bulk of the Script Elevation PowerToys are Vista-specific, CmdHere.inf and PowerShellHere.inf found in that download will work on Windows XP and Windows Server 2003.

Leave a Reply