{"id":15471,"date":"2011-02-27T00:01:00","date_gmt":"2011-02-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/02\/27\/use-powershell-to-add-commands-to-the-windows-explorer-command-bar\/"},"modified":"2011-02-27T00:01:00","modified_gmt":"2011-02-27T00:01:00","slug":"use-powershell-to-add-commands-to-the-windows-explorer-command-bar","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-add-commands-to-the-windows-explorer-command-bar\/","title":{"rendered":"Use PowerShell to Add Commands to the Windows Explorer Command Bar"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to add commands to the Windows Explorer command bar in Windows 7 and Windows Vista.<\/p>\n<h3>Weekend Scripter<\/h3>\n<p>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 <i>Windows 7 Inside Out<\/i> book for Microsoft Press. <\/p>\n<p>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, &ldquo;No problem&#8230;it would be a simple registry script.&rdquo; <\/p>\n<p>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. <\/p>\n<p>To make changes to the <b>Hkey_Local_Machine<\/b> registry hive, the Windows PowerShell ISE must be launched with administrator rights. I used my <b>Test-IsAdministrator<\/b> 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. <\/p>\n<p>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 <b>Task Items<\/b> 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. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0407.WES-2-27-11-01_30E15114.jpg\"><img decoding=\"async\" height=\"462\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3531.WES-2-27-11-01_thumb_59500D5B.jpg\" alt=\"Image of Add Folder Commands dialog box\" border=\"0\" title=\"Image of Add Folder Commands dialog box\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>The Windows Explorer command bar for the <b>Documents library<\/b> when items are selected is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4111.WES-2-27-11-02_1CD798B1.jpg\"><img decoding=\"async\" height=\"366\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7356.WES-2-27-11-02_thumb_39FD40BB.jpg\" alt=\"Image of Windows Explorer menu bar\" border=\"0\" title=\"Image of Windows Explorer menu bar\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>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 <strong>TaskItemsSelected<\/strong> Windows Explorer command bar for the <b>Documents Library<\/b>. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8420.WES-2-27-11-03_5B2D3697.jpg\"><img decoding=\"async\" height=\"424\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1884.WES-2-27-11-03_thumb_1801B86A.jpg\" alt=\"Image of deleted items\" border=\"0\" title=\"Image of deleted items\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>As you can see in the previous image, the GUIDs that are associated with the various libraries are not easily remembered&hellip;hence the need for the script. A semicolon separates each command in the registry key. <\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<blockquote>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$FormEvent_Load={<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$arrayCommands = Get-ChildItem -Path HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell | <\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">select-object pschildname | ForEach-Object{$_.pschildname.tostring()}<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">foreach($command in $arrayCommands)<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$ListBox2.items.add($command)<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">} #end foreach command<\/span><\/p>\n<\/blockquote>\n<p>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.<\/p>\n<blockquote>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$arrayFolders = &#8220;Documents&#8221;,&#8221;Music&#8221;,&#8221;Pictures&#8221;,&#8221;Videos&#8221;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">foreach($folder in $arrayFolders) {$listbox1.items.add($folder)}<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">} #end FormEvent_Load<\/span><\/p>\n<\/blockquote>\n<p>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 <b>listbox1<\/b>. I then use the <b>Switch<\/b> statement to choose the appropriate registry key. The radio buttons are used to help build the registry key. <\/p>\n<p>I then use the <b>Get-ItemProperty<\/b> 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 <b>selectedItems<\/b> from <b>ListBox2<\/b>,<b> <\/b>and I call <b>Set-Item<\/b> to write the value back to the registry. This portion of the script is shown here.<\/p>\n<blockquote>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$handler_btn_Add_Click={<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$textbox1.clear()<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$textbox1.refresh()<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$textbox1.resetText()<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">If(!(Test-IsAdministrator)) <\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">{ $Textbox1.text = &#8220;You must be an administrator to set registry values&#8221; ; start-sleep 2; $form1.close()}<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Foreach($listItem in $listbox1.SelectedItems)<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Switch ($listItem)<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">{<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">&#8220;Documents&#8221; {$regKey = &#8216;HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FolderTypes\\{fbb3477e-c9e4-4b3b-a2ba-d3f5d3cd46f9}&#8217; }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">&#8220;Music&#8221; {$regKey = &#8216;HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FolderTypes\\{3f2a72a7-99fa-4ddb-a5a8-c604edf61d6b}&#8217; }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">&#8220;Pictures&#8221; {$regKey = &#8216;HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FolderTypes\\{0b2baaeb-0042-4dca-aa4d-3ee8648d03e5}&#8217; }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">&#8220;Videos&#8221; {$regKey = &#8216;HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FolderTypes\\{631958a6-ad0f-4035-a745-28ac066dc6ed}&#8217; }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">&#8220;Generic&#8221; {$regKey = &#8216;HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FolderTypes\\{5c4f28b5-f869-4e84-8e60-f11db97c5cc7}&#8217; }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Default {$textbox1.text = &#8220;You must choose one of the items in the listbox&#8221; }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">} #end switch<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">} #end foreach listitem<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">If($radiobutton1.checked) { $regKey += &#8220;\\TasksItemsSelected&#8221; }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">If($radiobutton2.checked) { $regKey += &#8220;\\TasksNoItemsSelected&#8221; }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$DefaultValue = (Get-Itemproperty -path $regkey -name &#8220;(default)&#8221;).&#8221;(default)&#8221;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$DefaultValue += &#8220;;&#8221; + [string]$ListBox2.selectedItems -replace &#8221; &#8220;,&#8221;;&#8221;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$label1.text = &#8220;Modifying Registry:&#8221;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$label1.visible = $true<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">$textbox1.text = $regKey + &#8220;`r`n&#8221; + $DefaultValue<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Set-Item -Path $regKey -Value $DefaultValue -type String<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">} #end handler_btn_Add_Click<\/span><\/p>\n<\/blockquote>\n<p>The handler for the other button is similar to the one previously described, except that it does not write back to the registry&mdash;it merely displays the registry key and current value. <\/p>\n<p>The complete <a target=\"_blank\" href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/dc2867e3-4fa1-4b77-8c5a-ee6d27ab4ecf\">Set-ExplorerCommandBar.ps1 script<\/a> is posted to the Scripting Guys Script Repository.<\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,71,3,4,61,226,45],"class_list":["post-15471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-graphical","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-explorer","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15471"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15471\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}