The Deployment Guys

Helping to deploy your world automagically...

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

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

  • Comments 62
  • Likes

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 (http://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.

Attachment: PinItem.zip
  • Diogo

    You would have to use the TaskbarLinks setting mentioned above in unattend.xml to configure the desired initial taskbar links.

    Michael Murgolo

  • I would include the ZTIUtility.vbs in your zip download as other people have different uses for this than yours. I for one thought it didn't work until I found the additional file in a link later on.

    I wanted to use the script as I have a mandatory profile at work meaning my settings changes don't save on log off. With this I can pin my commonly used programs on startup.

  • Pete,

    Yes, I should have plainly stated the requirement that the script depends on ZTIUtility.vbs.  However, I will not redistribute files from other Microsoft products/downloads.  Those items have an End User License Agreement that must be accepted before using them.

    However, I have added the stand-alone PinItem.vbs script back to the Zip download.  This will be easier to use outside of MDT since it has no external dependencies.

    Michael Murgolo

  • Hello,

    thanks for the Scripts. But i can't get it to work. I'm using Windows 7 Pro (german)

    The PinItem.cmd creates a Pinned "Calc" in the Start-Menu, perfect. But in the Taskbar there will be no Calc pinned :-(

    The PinItem.vbs doesn't work. Neither in the StartMenu, nor in the Taskbar

    cscript PinItem.vbs /item:c:\temp\calc.lnk

    or

    cscript PinItem.vbs /item:%windir%\system32\calc.exe

    what am I doing wrong?

  • Thomas,

    I just tested them again on my machine (US English) and they worked fine.

    As I mentioned in the main blog text, these scripts were written for US English verbs.  The verbs for each action would have to be changed in the scripts for use with another language.

    Michael Murgolo

  • Hi Thomas,

    This script helped me out a lot customizing my Windows7 image but I also ran into the same issue using the script on a Windows 7 image with multiple languages installed.

    I am not a scripting king but I managed to write the following script. The script reads the current language variable from the registry. Based on this information it set a language verb variable called "PinItemtext"

    Just add the code into the script and replace the verb with "PinItemtext" and it should work.

    Here is the code:

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

    Option Explicit

    Dim objShell

    Dim strLangSet, strWinLogon, PinItemtext

    strLangSet = "LastUserLangID"

    strWinLogon = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ThemeManager\"

    Set objShell = CreateObject("WScript.Shell")

    strLangSet = objShell.RegRead(strWinLogon & strLangSet)

    Select case strLangSet

    case "1033"

    PinItemtext = "Pin to Taskbar"

    case "1031"

    PinItemtext = "An Taskleiste anheften"

    case "1040"

    PinItemtext = "Aggiungi alla barra delle applicazioni"

    case "1043"

    PinItemtext = "Aan de taakbalk vastmaken"

    case "1036"

    PinItemtext = "Épingler à la barre des tâches"

    case "1038"

    PinItemtext = "Rögzítés a tálcán"

    case else

    PinItemtext = "Pin to Taskbar"

    End Select

    Wscript.Echo PinItemtext

    WScript.Quit

    ---------

  • How do you UNpin the Windows Explorer icon from the taskbar?  I was able to remove IE and WMP but I keep getting an error on line 45 of the script below.

    [code]

    Option Explicit

    Const CSIDL_COMMON_PROGRAMS = &H17

    Const CSIDL_PROGRAMS = &H2

    Const CSIDL_STARTMENU = &HB

    Dim objShell

    Dim objAllUsersProgramsFolder

    Dim strAllUsersProgramsPath

    Dim objIEFolder

    Dim strIEFolderPath

    Dim objFolderWMP

    Dim objFolderItemWMP

    Dim objFolderIE

    Dim objFolderItemIE

    Dim objFolderExp

    Dim objFolderItemExp

    Dim colVerbs

    Dim objVerb

    Set objShell = CreateObject("Shell.Application")

    Set objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)

    strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path

    Set objIEFolder = objShell.NameSpace (CSIDL_STARTMENU)

    strIEFolderPath = objIEFolder.Self.Path

    'Windows Media Player

     Set objFolderWMP = objShell.Namespace(strAllUsersProgramsPath)

     Set objFolderItemWMP = objFolderWMP.ParseName("Windows Media Player.lnk")

     Set colVerbs = objFolderItemWMP.Verbs

     For Each objVerb in colVerbs

         If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt

     Next

    'Internet Explorer

    Set objFolderIE = objShell.Namespace(strIEFolderPath & "\Programs")

    Set objFolderItemIE = objFolderIE.ParseName("Internet Explorer.lnk")

    Set colVerbs = objFolderItemIE.Verbs

     For Each objVerb in colVerbs

         If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt

     Next

    'Windows Explorer

    Set objFolderExp = objShell.Namespace(strAllUsersProgramsPath & "\Accessories")

    Set objFolderItemExp = objFolderExp.ParseName("Windows Explorer.lnk")

    Set colVerbs = objFolderItemExp.Verbs

     For Each objVerb in colVerbs

         If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt

     Next

    [/code]

  • Hi @ The Deployment Guys ^^

    What do i have to change in the script or have to do, to be capaple of Pinning LNK-Files that are stored on a UNC-Path to the Taskbar or to the Starmenu? When i pinn Shorcuts that are stored on the Local Machine it works fine :). But when i want to Pinn Shorcuts from an a UNC-Path it happens nothing. I don't even get a failiure message >> " Item  pinned: True "

    I dont now what im doing wrong, ist that below the correct way of giving the command to the CMD-Promt??

    >> cscript.exe pinitem.vbs /item:"\\server\lnk\pdfcreator.lnk" /taskbar <<

    >> cscript.exe pinitem.vbs /item:\\server\lnk\pdfcreator.lnk /taskbar <<

    Thanks in advance!!

  • Wow am I glad I found this.  Thanks a lot guys.

    phoeneous thanks for your unpinning script too.  You almost had it.  The Windows Explorer link doesn't exist in strAllUsersProgramsPath.  It is in the User profile in the same location as strIEFolderPath.  I've changed that variable name to strUserFolderPath and using that to remove both IE and Windows Explorer as below:

    Option Explicit

    Const CSIDL_COMMON_PROGRAMS = &H17

    Const CSIDL_PROGRAMS = &H2

    Const CSIDL_STARTMENU = &HB

    Dim objShell

    Dim objAllUsersProgramsFolder

    Dim strAllUsersProgramsPath

    Dim objUserFolder

    Dim strUserFolderPath

    Dim objFolderWMP

    Dim objFolderItemWMP

    Dim objFolderIE

    Dim objFolderItemIE

    Dim objFolderExp

    Dim objFolderItemExp

    Dim colVerbs

    Dim objVerb

    Set objShell = CreateObject("Shell.Application")

    Set objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)

    strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path

    Set objUserFolder = objShell.NameSpace (CSIDL_STARTMENU)

    strUserFolderPath = objUserFolder.Self.Path

    'Windows Media Player

    Set objFolderWMP = objShell.Namespace(strAllUsersProgramsPath)

    Set objFolderItemWMP = objFolderWMP.ParseName("Windows Media Player.lnk")

    Set colVerbs = objFolderItemWMP.Verbs

    For Each objVerb in colVerbs

        If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt

    Next

    'Internet Explorer

    Set objFolderIE = objShell.Namespace(strUserFolderPath & "\Programs")

    Set objFolderItemIE = objFolderIE.ParseName("Internet Explorer.lnk")

    Set colVerbs = objFolderItemIE.Verbs

    For Each objVerb in colVerbs

       If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt

    Next

    'Windows Explorer

    Set objFolderExp = objShell.Namespace(strUserFolderPath & "\Programs\Accessories")

    Set objFolderItemExp = objFolderExp.ParseName("Windows Explorer.lnk")

    Set colVerbs = objFolderItemExp.Verbs

    For Each objVerb in colVerbs

       If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt

    Next

  • I am interested in using this in a Task Sequence that I use to deploy Office 2010.  I have been able to use the script on my computer to see that it is in fact pinning, but when I run the script in the task sequence I do not see them appearing in any user account that logs on to the computer.  I assume it is because the act of pinning has an effect on the current user profile in the context that the script is running in.  So I am wondering how do I apply this against the default user profile?  Can I?

  • MrShannon,

    There is really no way to pin an item for all users after the OS is deployed.  This would have to be done in something like a logon script.

    Michael Murgolo

  • This program is working fine for pinning programs but how would I need to modify it to pin a folder. I have seen and applied the regestry change that allows for the pinning of folders by holding down SHIFT key and right clicking then choosing Pin to Start Menu. I guess the main question is what modifications need to be made to the script to access this Alternative Command verbs list?

  • We have used this during our automated deployment, however the shortcuts are being overwritten when we do a user migration with USMT 4 is there any way of preventing the migration of these items?

    Regards

  • Is there a way to use a verb that only appears on the 'extended' context menu, ie, the context menu that appears when holding down shift+right-click? thanks!

  • Ian,

    I don't believe you can use the technique from my script to use a verb on the "extended" context menu.  The Shell Scripting Objects have not been updated to support those.

    Michael Murgolo

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment