Click here to Skip to main content
15,881,381 members
Articles / Programming Languages / VBScript

Programmatically PIN Shortcut onto Taskbar on Win7

Rate me:
Please Sign up or sign in to vote.
4.63/5 (7 votes)
25 Apr 2011CPOL2 min read 116.7K   8   3
Describes how to programmatically pin shortcut onto Taskbar on Windows 7

Background

During my work, I got one requirement of pinning a specific shortcut file (*.lnk) onto the Windows 7 Taskbar, after investigating I found programmatically achieving this is “not permitted”, refer to the MSDN article here.

A small set of applications are pinned by default for new installations. Other than these, only the user can pin further applications; programmatic pinning by an application is not permitted.

However, the exception comes from Windows Script Hosting, a snippet of VBscript can achieve my requirement, I read several articles (refer to the end of this post) and wrote two VBS scripts shown below:

Pin to Taskbar

VB.NET
Option Explicit

'Const CSIDL_COMMON_PROGRAMS = &H17
Dim ShellApp, FSO, Desktop
Set ShellApp = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")

'Set StartMenuFolder = ShellApp.NameSpace(CSIDL_COMMON_PROGRAMS)
Set Desktop =  ShellApp.NameSpace("C:\Users\Wayne\Desktop")

Dim LnkFile
LnkFile = Desktop.Self.Path&"\ScheduleNotifier.lnk"

If(FSO.FileExists(LnkFile)) Then
    Dim tmp, verb
    'For Each verb in Desktop.ParseName("ScheduleNotifier.lnk").Verbs
        'tmp = tmp&verb&chr(13)
    'Next
    'MsgBox(tmp)

    Dim desktopImtes, item
    Set desktopImtes = Desktop.Items()

    For Each item in desktopImtes
        If (item.Name = "ScheduleNotifier") Then
            'MsgBox(item.Name)
            For Each verb in item.Verbs
                If (verb.Name = "Pin to Tas&kbar") _
		Then 'If (verb.Name = "锁定到任务栏(&K)")
                    verb.DoIt
                End If
            Next
        End If
    Next

End If

Set FSO = Nothing
Set ShellApp = Nothing

Unpin from Taskbar

VB.NET
Option Explicit
'Const CSIDL_COMMON_PROGRAMS = &H17
Dim ShellApp, FSO, Desktop
Set ShellApp = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")

'Set StartMenuFolder = ShellApp.NameSpace(CSIDL_COMMON_PROGRAMS)
Set Desktop =  ShellApp.NameSpace("C:\Users\Wayne\Desktop")

Dim LnkFile
LnkFile = Desktop.Self.Path&"\ScheduleNotifier.lnk"

If(FSO.FileExists(LnkFile)) Then
    Dim tmp, verb
    'For Each verb in Desktop.ParseName("ScheduleNotifier.lnk").Verbs
        'tmp = tmp&verb&chr(13)
    'Next
    'MsgBox(tmp)

    Dim desktopImtes, item
    Set desktopImtes = Desktop.Items()

    For Each item in desktopImtes
        If (item.Name = "ScheduleNotifier") Then
            'MsgBox(item.Name)
            For Each verb in item.Verbs
                If (verb.Name = "Unpin from Tas&kbar") _
		Then 'If (verb.Name = "从任务栏脱离(&K)")
                    verb.DoIt
                End If
            Next
        End If
    Next

End If

Set FSO = Nothing
Set ShellApp = Nothing

Although two snippets of VBS code are simple and straight-forward, they have one serious limitation. It only supports one specified language, attention of this line of code:

VB.NET
If (verb.Name = "Pin to Tas&kbar") Then 'If (verb.Name = "锁定到任务栏(&K)")

On Chinese locale, I have to replace “Pin to Tas&kbar” with “锁定到任务栏(&K)”, so in order to support more languages, I need to know each exact {Pin sentence} to do the pinning, you know, Google translation won’t always work in this case:), so I definitely don’t want to do that…

You probably ask WHY??? Because FolderItemVerb object only has one property: Name, and Name is definitely “language sensitive“, MSDN does mention there are two more properties “Application” and “Parent” however they are “Not implemented”.

Several Tips

  • For a specific Windows user, all shortcuts pinned onto taskbar store the shortcuts files under
    %AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
  • OEM manufacturers could “pre-pin” some shortcut onto the Taskbar before shipping the PC/laptops to the consumers, simply run batch command below during the DASH (see Desktop and mobile Architecture for System Hardware) process:
    Rem Pining OEM Welcome Center app on task barReg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\TBDEn /v SBOEM0 /t REG_EXPAND_SZ /d “SomeFile.lnk” /f

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) SAP Labs Shanghai
China China
Wayne is a software developer, Tech Lead and also a geek. He has more than 6 years' experience in Web development(server: ASP.NET (MVC), Web Service, IIS; Client: HTML/CSS/JavaScript/jQuery/AJAX), Windows development (Winform, Windows Service, WPF/Silverlight, Win32 API and WMI) and SQL Server. Deep understanding of GOF Design Patterns, S.O.L.i.D principle, MVC, MVVM, Domain Driven Design, SOA, REST and AOP.

Wayne's Geek Life http://WayneYe.com

Infinite passion on programming!

Comments and Discussions

 
QuestionGerman ItemName Pin
largpack28-Jun-18 1:39
largpack28-Jun-18 1:39 
QuestionPinning files like cmd or html to start menu Pin
Bharathshivashankar19-Mar-12 2:47
Bharathshivashankar19-Mar-12 2:47 
AnswerRe: Pinning files like cmd or html to start menu Pin
Wayne Ye19-Mar-12 18:42
Wayne Ye19-Mar-12 18:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.