Pin and Unpin Start Menu and Taskbar Items in Windows 7October 2, 2013 6:30 am ·
Thanks to RobSampson for submitting this awesome tip.
For exclusive access to thousands of other Windows solutions, join the Experts Exchange community for free today!
I have seen many people talk about a requirement to efficiently and easily pin and unpin items from the Start Menu and Taskbar for users on Windows 7 operating systems. I have developed a script in VBScript that goes a long way to making this task as easy as possible.
The script I have developed allows the user to modify only one section of the code, in which they detail the operation, the target, and the shortcut to be created, all at once, so multiple operations can be performed with one run of the script.
The code went through a few iterations, but I ended up with this version, which is a very flexible version for system administrators. The full code is shown below.
' Specify the file paths of the file to pin or unpin ' The second element on each line of the array can be either "Both", "Start Menu", or ' "Taskbar" to specify the target of each operation. ' If strMode = "unpin", you can use "unpin_all" in the third element of the array to unpin ' all items from the Taskbar or Start Menu. ' SYNTAX IS , , WITH THREE ELEMENTS PER ACTION arrActions = Array( _ "unpin", "Both", "unpin_all", _ "pin", "Start Menu", "C:\Windows\Notepad.exe", _ "pin", "Taskbar", "C:\Windows\Calc.exe" _ )
For intAction = 0 To (UBound(arrActions) - 2) Step 3
arrFileNames = Array(arrActions(intAction + 2))
'strMode can be "Pin" or "Unpin" strMode = arrActions(intAction)
'strLocation can be "Start Menu" or "Taskbar" or "Both" strLocation = arrActions(intAction + 1)
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objShellApp = CreateObject("Shell.Application") Set objShell = CreateObject("WScript.Shell") If LCase(strLocation) = "both" Then arrLocations = Array("Start Menu", "Taskbar") Else arrLocations = Array(strLocation) End If
For Each strLocation In arrLocations If LCase(strMode) <> "pin" And LCase(strMode) <> "unpin" Then WScript.Echo "Mode is incorrect. Please set to ""pin"" or ""unpin""." WScript.Quit ElseIf LCase(strLocation) <> "start menu" And LCase(strLocation) <> "taskbar" Then WScript.Echo "Location is incorrect. Please set to ""Start Menu"" or ""Taskbar""." WScript.Quit Else strMode = LCase(strMode) If strMode = "pin" Then strVerb = LCase(strMode & " to " & strLocation) strMessage = " has been " & strMode & "ned to the " & strLocation & "." ElseIf strMode = "unpin" Then strVerb = LCase(strMode & " from " & strLocation) strMessage = " has been " & strMode & "ned from the " & strLocation & "." End If
For Each strFilePath In arrFileNames If LCase(strFilePath) = "unpin_all" And strMode = "unpin" Then strPinLocation = objShell.ExpandEnvironmentStrings("%APPDATA%") & _ "\Microsoft\Internet Explorer\Quick Launch\User Pinned\" & _ Replace(strLocation, " ", "") & "\" For Each objFile In objFSO.GetFolder(strPinLocation).Files strFullPath = objFile.Path 'Set objFile = objFSO.GetFile(objFile.Path) Set objFolder = objShellApp.Namespace(objFile.ParentFolder & "\") Set objFolderItem = objFolder.ParseName(objFile.Name) Set colVerbs = objFolderItem.Verbs For Each objVerb In colVerbs If LCase(Replace(objVerb.name, "&", "")) = strVerb Then objVerb.DoIt WScript.Echo strFullPath & strMessage End If Next Next Else If objFSO.FileExists(strFilePath) = True Then Set objFile = objFSO.GetFile(strFilePath) Set objFolder = objShellApp.Namespace(objFile.ParentFolder & "\") Set objFolderItem = objFolder.ParseName(objFile.Name) Set colVerbs = objFolderItem.Verbs blnOptionFound = False For Each objVerb In colVerbs If LCase(Replace(objVerb.name, "&", "")) = strVerb Then objVerb.DoIt blnOptionFound = True End If Next If blnOptionFound = True Then WScript.Echo strFilePath & strMessage Else WScript.Echo "Unable to " & strMode & " " & strFilePath & _ " from the " & strLocation & ". The verb does not exist." End If Else WScript.Echo "Could not find " & strFilePath End If End If Next End If Next Next
The first 15 lines of that code block take care of the special case requirement to unpin all items from the specified location. It binds to the folder used for the user pinned items to a location, and performs the verb “Unpin from <location>” on each file in that location.The final block of code in that section takes care of the pinning or unpinning of individual items from the specified location, again by performing the verb “Pin to <location>” or “Unpin from <location>” on each file path specified.
Finally, we close out the For and If statements used within the code.
Next End If Next Next
And that’s it. I hope this script is useful to any system administrators wanting to easily manage user pinned items. Download the code in in a .txt file here: Pin and Unpin Start Menu and Taskbar items in Windows 7
For exclusive access to thousands of Windows solutions, articles and experts, join the Experts Exchange community for free today!