February 27th, 2011

Use PowerShell to Add Commands to the Windows Explorer Command Bar

Summary: Learn how to use Windows PowerShell to add commands to the Windows Explorer command bar in Windows 7 and Windows Vista.

Weekend Scripter

Microsoft Scripting Guy, Ed Wilson, here. Did you ever have one of those times when a seemingly simple project suddenly morphed into a monster project? It seems that when I have a bit of extra time, I let the project get out of hand. If I am under a tight deadline, I follow a more straight line. A case in point is the script I just finished writing for the Second Edition of the Windows 7 Inside Out book for Microsoft Press.

I was asked to supply around a hundred scripts or so, and I did that. But I also told the authors that if they needed any other scripts to let me know. I received an email asking about adding commands to the explorer bar. At first I thought, “No problem…it would be a simple registry script.”

Then I thought about the different commands that could be added, and the different combinations that might be involved in registry locations, and I decided that a graphical script would be a better way to approach the problem.

To make changes to the Hkey_Local_Machine registry hive, the Windows PowerShell ISE must be launched with administrator rights. I used my Test-IsAdministrator function to check this prior to attempting to make any changes. However, you might be interested in only reporting the values that are contained in the registry, and read-only access is permitted.

As shown in the following image, when the Set-ExplorerCommandBar.ps1 script runs, a WinForm appears. The four standard Windows 7 libraries appear in the upper left box. The upper right box is a multi-select box that allows you to choose which commands to add to the Windows Explorer command bar. The Task Items group contains two radio buttons that allow you to choose if items are selected in the library or if items are not selected. The lower text box displays the currently selected registry key and the current commands that are enabled for that particular library.

Image of Add Folder Commands dialog box

The Windows Explorer command bar for the Documents library when items are selected is shown in the following image.

Image of Windows Explorer menu bar

By removing or by adding commands to the registry keys, the commands that appear on the Windows Explorer command bar will change. In the following image, I delete a few commands from the TaskItemsSelected Windows Explorer command bar for the Documents Library.

Image of deleted items

As you can see in the previous image, the GUIDs that are associated with the various libraries are not easily remembered…hence the need for the script. A semicolon separates each command in the registry key.

In this version of the script, I do not support removing commands, only adding them. In addition, there is no backup, so you should ensure that you have a proper backup of the registry prior to running the script.

When the script runs, the first thing that happens is the WinForm loads. In the Form.Load event, I query the registry to retrieve all the available folder commands and store them in an array. I then walk through the array and add each of the commands to the list box. This portion of the script is shown here.

$FormEvent_Load={

$arrayCommands = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell |

select-object pschildname | ForEach-Object{$_.pschildname.tostring()}

foreach($command in $arrayCommands)

{

$ListBox2.items.add($command)

} #end foreach command

The other thing that happens in the Form.Load event is that I populate ListBox1 with the names of the four default libraries. This portion of the script is shown here.

$arrayFolders = “Documents”,”Music”,”Pictures”,”Videos”

foreach($folder in $arrayFolders) {$listbox1.items.add($folder)}

} #end FormEvent_Load

If the add button is pressed, it will generate a button click event. I clear the text box, check to see if the person is an administrator, and then see what library is selected in listbox1. I then use the Switch statement to choose the appropriate registry key. The radio buttons are used to help build the registry key.

I then use the Get-ItemProperty cmdlet to retrieve the value of the registry key. Next, I build up the value to add to the registry key by converting the array of selectedItems from ListBox2, and I call Set-Item to write the value back to the registry. This portion of the script is shown here.

$handler_btn_Add_Click={

$textbox1.clear()

$textbox1.refresh()

$textbox1.resetText()

If(!(Test-IsAdministrator))

{ $Textbox1.text = “You must be an administrator to set registry values” ; start-sleep 2; $form1.close()}

Foreach($listItem in $listbox1.SelectedItems)

{

Switch ($listItem)

{

“Documents” {$regKey = ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes\{fbb3477e-c9e4-4b3b-a2ba-d3f5d3cd46f9}’ }

“Music” {$regKey = ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes\{3f2a72a7-99fa-4ddb-a5a8-c604edf61d6b}’ }

“Pictures” {$regKey = ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes\{0b2baaeb-0042-4dca-aa4d-3ee8648d03e5}’ }

“Videos” {$regKey = ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes\{631958a6-ad0f-4035-a745-28ac066dc6ed}’ }

“Generic” {$regKey = ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderTypes\{5c4f28b5-f869-4e84-8e60-f11db97c5cc7}’ }

Default {$textbox1.text = “You must choose one of the items in the listbox” }

} #end switch

} #end foreach listitem

If($radiobutton1.checked) { $regKey += “\TasksItemsSelected” }

If($radiobutton2.checked) { $regKey += “\TasksNoItemsSelected” }

$DefaultValue = (Get-Itemproperty -path $regkey -name “(default)”).”(default)”

$DefaultValue += “;” + [string]$ListBox2.selectedItems -replace ” “,”;”

$label1.text = “Modifying Registry:”

$label1.visible = $true

$textbox1.text = $regKey + “`r`n” + $DefaultValue

Set-Item -Path $regKey -Value $DefaultValue -type String

} #end handler_btn_Add_Click

The handler for the other button is similar to the one previously described, except that it does not write back to the registry—it merely displays the registry key and current value.

The complete Set-ExplorerCommandBar.ps1 script is posted to the Scripting Guys Script Repository.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Author

0 comments

Discussion are closed.