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:
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)
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")
TempShortcutLocation = FileSystemObj.GetFolder(ObjShell.ExpandEnvironmentStrings("%TEMP%"))
If(trim(lcase(OnStartMenu)) = "true") then 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
TempShortcut = TempShortcutLocation & "\" & ShortcutName & ".lnk"
HasItAlreadyBeenPinnedShortCut = HasItAlreadyBeenPinnedShortCut _
& "\" & ShortcutName & ".lnk"
If(FileSystemObj.FileExists(HasItAlreadyBeenPinnedShortCut)) Then
Set ObjShell = Nothing
Set FileSystemObj = Nothing
Exit Sub
End If
Set lnk = ObjShell.CreateShortcut(TempShortcut)
lnk.TargetPath = AppPathAndName lnk.Arguments = Arguments lnk.Description = ShortcutName lnk.Save
Set ObjShellApp = CreateObject("Shell.Application")
Set ShortCutToPin = ObjShellApp.NameSpace(TempShortcutLocation)
If(FileSystemObj.FileExists(TempShortcut)) Then
Dim ShortCutToPinItem, verb
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()
short-cut name matches the passed ShortcutName argument
If (ShortCutToPinItem.Name = ShortcutName) Then
(the short-cut) commands looking for the pinning method.
For Each verb in ShortCutToPinItem.Verbs
so pin it to verb If (verb.Name = verbToDo) Then verb.DoIt
Next
End If
Next
FileSystemObj.DeleteFile(TempShortcut)
End If
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:
- Full application name and path
- Name for the new pinned item
- A boolean to pin onto the Start Menu (
true
) or Taskbar (false
)
- Arguments for the new link itself
Using the procedure example:
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
- Programmatically PIN shortcut onto Taskbar on Win 7 by Wayne Ye
- Pin and unpin applications from the taskbar and Start-menu by Jan Egil Ring, Crayon
- MSDN Shell objects for scripting