Pin Items to the Start Menu or Windows 7 Taskbar via Script

Update 28 April 2009 – This post originally had the sample scripts pinning the executable (calc.exe) directly.  A colleague of mine pointed out that it would be better to pin the Start Menu shortcuts for items instead of directly pinning the executable.  This should be done because shortcuts for Windows Installer applications are special.  Launching applications using their Windows Installer shortcuts can, for example, initiate a repair of the application it if is needed.  So I have changed all the samples to point to shortcuts.  I have also rewritten the attached script as an MDT script and add added a function library that allows the CSIDL (constant special item ID list) values for “special” folders to be used a variables in the item path.

--------------------------------------------------------------------------------------

Many customers wish to pre-configure items that are “pinned” to the Start Menu in their Windows images.  Also, since items can now be pinned to the new Taskbar in Windows 7, customers will want to configure “pinned” items there as well.

There is no direct programmatic interface to add pinned items to either the Start Menu or Windows 7 Taskbar.  This was done deliberately to prevent installation programs from spamming these locations with their icons (https://blogs.msdn.com/oldnewthing/archive/2003/09/03/54760.aspx).  This caused many customers to have to take manual steps in the image build process to configure pinned items.  However, there is an indirect way to automate this by using the Shell Objects for Scripting.

When you right click on an object (e.g. file or folder icon) in Explorer, you are presented with a menu of actions like Open, Copy, Create Shortcut.  These actions are called verbs in Shell speak.  The Shell Objects for Scripting allow you to enumerate and execute these verb.  Here is a small snippet of code showing how to enumerate the verbs for Calculator:

Const CSIDL_COMMON_PROGRAMS = &H17
Const CSIDL_PROGRAMS = &H2
Set objShell = CreateObject("Shell.Application")
Set objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)
strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path
Set objFolder = objShell.Namespace(strAllUsersProgramsPath & "\Accessories")
Set objFolderItem = objFolder.ParseName("Calculator.lnk")
Set colVerbs = objFolderItem.Verbs
For Each objVerb in colVerbs
Wscript.Echo objVerb
Next

Below is the output of a small command line script that I wrote (ListVerbs.vbs, included in the attachment) to list the verbs of an item like Calculator.

Verbs2

As you can see, the verbs Pin to Tas&kbar and Pin to Start Men&u are available as verbs for Calculator on my Windows 7 machine. (The & in the verb precedes the letter that can be used to select that verb from the menu using the keyboard.)  We can therefore use the Shell Objects for Scripting to programmatically execute these verbs.  Below is a snippet of VBScript showing how to pin Calculator to the Start Menu:

Const CSIDL_COMMON_PROGRAMS = &H17
Const CSIDL_PROGRAMS = &H2
Set objShell = CreateObject("Shell.Application")
Set objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)
strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path
Set objFolder = objShell.Namespace(strAllUsersProgramsPath & "\Accessories")
Set objFolderItem = objFolder.ParseName("Calculator.lnk")
Set colVerbs = objFolderItem.Verbs
For Each objVerb in colVerbs
If Replace(objVerb.name, "&", "") = "Pin to Start Menu" Then objVerb.DoIt
Next

I’ve included in the attachment for this post a switch-driven MDT script, PinItem.wsf, that can be used to pin items to the Start Menu or Windows 7 Taskbar.  This can be used during image builds and these additions do survive the automated profile copy mechanisms in XP and Vista (haven’t tested Windows 7 yet but it should work).  It can also be used in logon scripts, etc.  Please note that this script was written for US English verbs.  The verbs for each action would have to be changed in the script for use with another language.

For automated deployments, some of these items can also be configured through an answer file on Windows Vista and higher.  Windows 7 provides an unattend.xml setting to configure up to three Taskbar pinned items (see TaskbarLinks in Microsoft-Windows-Shell-Setup in the Automated Installation Kit documentation).  And both Windows Vista and Windows 7 provide an unattend.xml setting to configure up to five “recently opened programs” on the Start Menu (StartPanelLinks in Microsoft-Windows-Shell-Setup).  However, neither provide a way in unattend.xml to pin items to the Start Menu.

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use .

This post was contributed by Michael Murgolo, a Senior Consultant with Microsoft Services - U.S. East Region.

PinItem.zip