Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / VBScript
Tip/Trick

Pin a Shortcut onto the Taskbar or Start Menu

Rate me:
Please Sign up or sign in to vote.
4.86/5 (8 votes)
8 Oct 2014CPOL2 min read 58.8K   495   11   19
Pin a shortcut onto the Taskbar or Start Menu using VBScript

Introduction

Update: THIS SCRIPT WILL NOT WORK ON WINDOWS 10.

This script solves the problem of pinning applications (adding short-cuts) onto the task bar and start menu with no user interaction. This can be especially useful to System Administrators in a domain environment looking to add short-cuts to a profile with no user interaction. I find this solution is best implemented with Group Policy Object's (GPO) logon script but it can be used independently as well.

Background

The code works by passing parameters to a sub procedure with an application path & name, a name for the future pinned item, and a boolean directing the location of pinned item. The first step creates a short-cut in a temporary location. This is a workaround necessity to create a short-cut for applications that will not be pinned directly. The next step takes that short-cut and loops through its shell object's verbs and selects the pinned item the boolean selected pinned location. Finally, it selects the matched verb and pins the application to the selected pinned location.

Here is the full script in VBScript:

VBScript
'Pin an application to a start menu or task bar
If WScript.Arguments.Count = 4 then
	Call PinApplicationToTaskBar(WScript.Arguments(0), _
	WScript.Arguments(1), WScript.Arguments(2), WScript.Arguments(3))
Else
	WScript.Echo "Missing parameters.  _
	String AppPathAndName String ShortcutName Boolean OnStartMenu." _
	& vbCr & vbCr & "  Example cmd.exe CMD  false " _
	& vbCr & vbCr & "  Example %windir%\system32\SnippingTool.exe _
	SnipIt false " & chr(34) &  " "  & chr(34) 
End If
	
Public Sub PinApplicationToTaskBar(AppPathAndName, ShortcutName, OnStartMenu, Arguments)
	'This is on for a soft failure. _
	Uncomment this if error checking for a hard failure is needed for debugging.
	On Error Resume Next

	Dim FileSystemObj, ObjShell, ObjShellApp
	Set ObjShell = WScript.CreateObject("WScript.Shell")
	Set FileSystemObj = CreateObject("Scripting.FileSystemObject")
	
	'Create a temp location for the short-cut to exist
	TempShortcutLocation = FileSystemObj.GetFolder(ObjShell.ExpandEnvironmentStrings("%TEMP%"))
	'Where is it being pinned too?  Determine the location where the pinned item will reside.
	If(trim(lcase(OnStartMenu)) = "true") then ' pinned to start menu
		HasItAlreadyBeenPinnedShortCut = ObjShell.ExpandEnvironmentStrings_
		("%APPDATA%") & _
		"\Microsoft\Internet Explorer\Quick Launch\User Pinned\StartMenu"
	Else
		HasItAlreadyBeenPinnedShortCut = ObjShell.ExpandEnvironmentStrings_
		("%APPDATA%") & _
		"\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"
	End If
	'Temporary location for the application short-cut
	TempShortcut = TempShortcutLocation & "\" & ShortcutName & ".lnk"
	'Possible location of a pinned item
	HasItAlreadyBeenPinnedShortCut =  HasItAlreadyBeenPinnedShortCut _
	& "\" & ShortcutName & ".lnk"
	'If this already exists, than exit this procedure. The application has already been pinned.
	If(FileSystemObj.FileExists(HasItAlreadyBeenPinnedShortCut)) Then
		'MsgBox(HasItAlreadyBeenPinnedShortCut & " Already Pinned")
		Set ObjShell = Nothing
		Set FileSystemObj = Nothing
		Exit Sub
	End If
	'Create a short-cut using the shell
	Set lnk = ObjShell.CreateShortcut(TempShortcut)
	lnk.TargetPath = AppPathAndName ' Full application path and name
	lnk.Arguments = Arguments 'Arguments wanted for link
	lnk.Description = ShortcutName 'The name that appears on the start menu.
	lnk.Save 
	
	Set ObjShellApp = CreateObject("Shell.Application")
		
	'Get the newly created short-cut full path
	Set ShortCutToPin =  ObjShellApp.NameSpace(TempShortcutLocation) 
			
	If(FileSystemObj.FileExists(TempShortcut)) Then 
		Dim ShortCutToPinItem, verb
		'Set the location to pin the item to do based on the passed OnStartMenu argument
		If(trim(lcase(OnStartMenu)) = "true") then
			verbToDo = "Pin to Start Men&u"
		Else	
			verbToDo = "Pin to Tas&kbar"
		End If
		For Each ShortCutToPinItem in ShortCutToPin.Items()
			'Look for the pinning verb when the temporary _
			short-cut name matches the passed ShortcutName argument
			If (ShortCutToPinItem.Name = ShortcutName) Then
				'Loop through the shell object's _
				(the short-cut) commands looking for the pinning method.
				For Each verb in ShortCutToPinItem.Verbs 
					'The verb matches the verbToDo _
					so pin it to verb's defined location
					If (verb.Name = verbToDo) Then verb.DoIt
				Next
			End If
		Next
		'Delete the temporary short-cut used to pin the application
		FileSystemObj.DeleteFile(TempShortcut) 
	End If
	'clean up
	Set ObjShell =  Nothing
	Set FileSystemObj = Nothing
	Set ObjShellApp = Nothing
End Sub	

Using the Code

The best way to create a pinned application is by calling the "PinApplicationToTaskBar" procedure.

There are four arguments that need to be passed to this script:

  1. Full application name and path
  2. Name for the new pinned item
  3. A boolean to pin onto the Start Menu (true) or Taskbar (false)
  4. Arguments for the new link itself

Using the procedure example:

VBScript
Call PinApplicationToTaskBar("C:\WINDOWS\system32\SnippingTool.exe", _
"Snipping Tool", "true","")

Using the command line:

PinToBar.vbs AppPathAndName ShortcutName OnStartMenu Arguments

Pinning the Snipping Tool onto the Taskbar:

C:\PinToBar.vbs C:\WINDOWS\system32\SnippingTool.exe "Snipping Tool" false ""

Pinning a Command Prompt onto the Start Menu:

C:\PinToBar.vbs C:\WINDOWS\system32\cmd.exe "Command Prompt" true ""

Pinning Internet Explorer with Google as the start page onto the Taskbar:

PinToBar.vbs "c:\Program Files (x86)\Internet Explorer\iexplore.exe" 
"Google" false "http://www.google.com/"

References

  1. Programmatically PIN shortcut onto Taskbar on Win 7 by Wayne Ye
  2. Pin and unpin applications from the taskbar and Start-menu by Jan Egil Ring, Crayon
  3. MSDN Shell objects for scripting

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)
United States United States
Spencer is a software engineer, database designer, and general tinker of applications. Sometimes hardware too! He can’t decide whether Linux or Windows environment to use so he uses both consistently.

Been programming long enough for his peers to call him a professional "software engineer". Primarily a full stack .NET developer and frequently wanders over to NodeJs. Most days are spent developing WebApis, JavaScript, and building SPA sites.

Comments and Discussions

 
GeneralDoesn't seem to be pinning to start under Windows 8.1 Pin
dwmyersjr28-Oct-15 3:57
dwmyersjr28-Oct-15 3:57 
QuestionTarget path Pin
Member 1175974311-Jun-15 11:09
Member 1175974311-Jun-15 11:09 
Got this script to run via *.bat file. Is there a way to set the target path to a file instead of a program?

Example:
Target path
"c:\program files (x86)\internet explorer\iexplore.exe" http://google.com/

or

"C:\Program Files (x86)\Microsoft Office\Office14\Excel.exe" C:\users\User1\desktop\calendar.xlsx

Interested in setting the target path in the shortcut to a file location to open the specified file. Unable to work with the shortcut itself on the local system due to restrictions set by GPO.
AnswerRe: Target path Pin
Spencer Kittleson15-Jun-15 10:35
professionalSpencer Kittleson15-Jun-15 10:35 
QuestionDoesn't work on Windows Server 2012 R2? Pin
highend027-Nov-14 19:15
highend027-Nov-14 19:15 
QuestionWhere to enter the code for desired apps... Pin
Member 111363077-Oct-14 11:34
Member 111363077-Oct-14 11:34 
AnswerRe: Where to enter the code for desired apps... Pin
Spencer Kittleson8-Oct-14 4:56
professionalSpencer Kittleson8-Oct-14 4:56 
AnswerRe: Where to enter the code for desired apps... Pin
Spencer Kittleson8-Oct-14 6:40
professionalSpencer Kittleson8-Oct-14 6:40 
GeneralRe: Where to enter the code for desired apps... Pin
Huanwei Li6-Jul-15 21:45
Huanwei Li6-Jul-15 21:45 
GeneralRe: Where to enter the code for desired apps... Pin
Spencer Kittleson7-Jul-15 10:34
professionalSpencer Kittleson7-Jul-15 10:34 
GeneralRe: Where to enter the code for desired apps... Pin
Huanwei Li7-Jul-15 18:34
Huanwei Li7-Jul-15 18:34 
GeneralRe: Where to enter the code for desired apps... Pin
Spencer Kittleson9-Jul-15 16:43
professionalSpencer Kittleson9-Jul-15 16:43 
GeneralRe: Where to enter the code for desired apps... Pin
Huanwei Li9-Jul-15 18:24
Huanwei Li9-Jul-15 18:24 
QuestionUnpin from taskbar Pin
vn.9xpro19-Sep-14 3:38
vn.9xpro19-Sep-14 3:38 
AnswerRe: Unpin from taskbar Pin
Spencer Kittleson7-Oct-14 18:52
professionalSpencer Kittleson7-Oct-14 18:52 
QuestionErrors Pin
Member 301751411-Jun-14 2:22
Member 301751411-Jun-14 2:22 
SuggestionRe: Errors Pin
Spencer Kittleson11-Jun-14 17:39
professionalSpencer Kittleson11-Jun-14 17:39 
GeneralRe: Errors Pin
Member 1090172623-Jun-14 12:22
Member 1090172623-Jun-14 12:22 
GeneralRe: Errors Pin
Spencer Kittleson1-Jul-14 13:04
professionalSpencer Kittleson1-Jul-14 13:04 
GeneralMy vote of 5 Pin
Wooters22-Jan-14 19:43
Wooters22-Jan-14 19:43 

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.