-
-
Save sirlancelot/368910 to your computer and use it in GitHub Desktop.
@echo off | |
cscript PinItem.vbs /taskbar /item:"%ProgramData%\Microsoft\Windows\Start Menu\Programs\Mozilla Firefox\Mozilla Firefox.lnk" | |
cscript PinItem.vbs /taskbar /item:"%ProgramData%\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office Word 2007.lnk" | |
cscript PinItem.vbs /taskbar /item:"%ProgramData%\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office Excel 2007.lnk" | |
cscript PinItem.vbs /taskbar /item:"%ProgramData%\Microsoft\Windows\Start Menu\Programs\Microsoft Office\Microsoft Office PowerPoint 2007.lnk" | |
echo. | |
echo ---------- | |
echo Deleting Self... | |
del %0 | |
pause |
' Windows Script Host Sample Script | |
' | |
' ------------------------------------------------------------------------ | |
' Copyright (C) 2009 Microsoft Corporation | |
' | |
' You have a royalty-free right to use, modify, reproduce and distribute | |
' the Sample Application Files (and/or any modified version) in any way | |
' you find useful, provided that you agree that Microsoft and the author | |
' have no warranty, obligations or liability for any Sample Application Files. | |
' ------------------------------------------------------------------------ | |
'******************************************************************** | |
'* | |
'* File: PinItem.vbs | |
'* Date: 03/04/2009 | |
'* Version: 1.0.2 | |
'* | |
'* Main Function: VBScipt to pin an item to the Start Menu or Taskbar | |
'* | |
'* Usage: cscript PinItem.vbs /item:<path to exe> | |
'* [/taskbar] [/?] | |
'* | |
'* Copyright (C) 2009 Microsoft Corporation | |
'* | |
'* Revisions: | |
'* | |
'* 1.0.0 - 04/03/2008 - Created. | |
'* 1.0.1 - 03/02/2009 - Used Replace in PinItem function to remove "&" | |
'* from verb. | |
'* 1.0.2 - 03/04/2009 - Script name was PinToStartMenu.vbs. Added | |
'* /taskbar switch to pin items to taskbar on | |
'* Win7. | |
'* | |
'******************************************************************** | |
'***************************************************************************** | |
'* Declare Variables | |
'***************************************************************************** | |
Option Explicit | |
'On Error Resume Next | |
Dim blnPinned | |
Dim blnTaskbar | |
Dim i | |
Dim intOpMode | |
Dim objWshShell | |
Dim objFSO | |
Dim objShell | |
Dim strPath | |
Dim strArguments | |
Dim strOptionsMessage | |
' Define constants | |
Const CONST_ERROR = 0 | |
Const CONST_WSCRIPT = 1 | |
Const CONST_CSCRIPT = 2 | |
Const CONST_SHOW_USAGE = 3 | |
Const CONST_PROCEED = 4 | |
Const CONST_STRING_NOT_FOUND = -1 | |
Const CONST_FOR_READING = 1 | |
Const CONST_FOR_WRITING = 2 | |
Const CONST_FOR_APPENDING = 8 | |
Const CONST_Success = 0 | |
Const CONST_Failure = 1 | |
Const TRISTATE_USE_DEFAULT = -2 | |
Const TRISTATE_TRUE = -1 'Open the file as Unicode. | |
Const TRISTATE_FALSE = 0 'Open the file as ASCII. | |
blnTaskbar = False | |
'***************************************************************************** | |
'* Create Objects | |
'***************************************************************************** | |
Set objWshShell = CreateObject("Wscript.Shell") | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
Set objShell = CreateObject("Shell.Application") | |
'******************************************************************** | |
'* Check script host exe and parse command line | |
'******************************************************************** | |
'Get the command line arguments | |
For i = 0 to Wscript.arguments.count - 1 | |
ReDim Preserve arrArguments(i) | |
arrArguments(i) = Wscript.arguments.item(i) | |
Next | |
'Check whether the script is run using CScript | |
Select Case intChkProgram() | |
Case CONST_CSCRIPT | |
'Do Nothing | |
Case CONST_WSCRIPT | |
WScript.Echo "Please run this script using CScript." & vbCRLF & _ | |
"This can be achieved by" & vbCRLF & _ | |
"1. Using ""CScript MODIFYUSERS.vbs arguments"" for Windows 95/98 or" & VbCrLf & _ | |
"2. Changing the default Windows Scripting Host setting to CScript" & vbCRLF & _ | |
" using ""CScript //H:CScript //S"" and running the script using" & vbCRLF & _ | |
" ""MODIFYUSERS.vbs arguments"" for Windows NT." | |
WScript.Quit | |
Case Else | |
WScript.Quit | |
End Select | |
'Parse the command line | |
Err.Clear() | |
intOpMode = intParseCmdLine(arrArguments, strPath, blnTaskbar, strOptionsMessage) | |
If Err.Number Then | |
Wscript.Echo "Error 0X" & CStr(Hex(Err.Number)) & " occurred in parsing the command line." | |
If Err.Description <> "" Then | |
Wscript.Echo "Error description: " & Err.Description & "." | |
End If | |
'WScript.quit | |
End If | |
Select Case intOpMode | |
Case CONST_SHOW_USAGE | |
Call ShowUsage() | |
WScript.quit | |
Case CONST_PROCEED | |
'Do nothing. | |
Case CONST_ERROR | |
WScript.quit | |
Case Else | |
Wscript.Echo "Error occurred in passing parameters." | |
End Select | |
'******************************************************************** | |
'* Main Script | |
'******************************************************************** | |
WScript.Echo strOptionsMessage | |
blnPinned = PinItem(strPath, blnTaskbar) | |
WScript.Echo "Item pinned: " & CStr(blnPinned) | |
If blnPinned Then | |
WScript.Quit(0) | |
Else | |
WScript.Quit(1) | |
End If | |
'******************************************************************** | |
'* | |
'* Function intChkProgram() | |
'* | |
'* Purpose: Determines which program is used to run this script. | |
'* | |
'* Input: None | |
'* | |
'* Returns: intChkProgram is set to one of CONST_ERROR, CONST_WSCRIPT, | |
'* and CONST_CSCRIPT. | |
'* | |
'******************************************************************** | |
Private Function intChkProgram() | |
ON ERROR RESUME NEXT | |
Dim i | |
Dim j | |
Dim strFullName | |
Dim strCommand | |
'strFullName should be something like C:\WINDOWS\COMMAND\CSCRIPT.EXE | |
strFullName = WScript.FullName | |
If Err.Number then | |
Wscript.Echo "Error 0x" & CStr(Hex(Err.Number)) & " occurred." | |
If Err.Description <> "" Then | |
Wscript.Echo "Error description: " & Err.Description & "." | |
End If | |
intChkProgram = CONST_ERROR | |
Exit Function | |
End If | |
i = InStr(1, strFullName, ".exe", 1) | |
If i = 0 Then | |
intChkProgram = CONST_ERROR | |
Exit Function | |
Else | |
j = InStrRev(strFullName, "\", i, 1) | |
If j = 0 Then | |
intChkProgram = CONST_ERROR | |
Exit Function | |
Else | |
strCommand = Mid(strFullName, j+1, i-j-1) | |
Select Case LCase(strCommand) | |
Case "cscript" | |
intChkProgram = CONST_CSCRIPT | |
Case "wscript" | |
intChkProgram = CONST_WSCRIPT | |
Case Else 'should never happen | |
Wscript.Echo "An unexpected program is used to run this script." | |
Wscript.Echo "Only CScript.Exe or WScript.Exe can be used to run this script." | |
intChkProgram = CONST_ERROR | |
End Select | |
End If | |
End If | |
End Function | |
'******************************************************************** | |
'* | |
'* Function intParseCmdLine() | |
'* | |
'* Purpose: Parses the command line. | |
'* | |
'* Input: arrArguments An array containing input from the command line | |
'* | |
'* Input: strPath Path of exe to pin | |
'* strOptionsMessage String containing options selected | |
'* | |
'* Returns: intParseCmdLine is set to one of CONST_ERROR, CONST_SHOW_USAGE, | |
'* and CONST_PROCEED. | |
'* | |
'******************************************************************** | |
Private Function intParseCmdLine(arrArguments, strPath, blnTaskbar, strOptionsMessage) | |
ON ERROR RESUME NEXT | |
Dim i | |
Dim strFlag | |
Dim strSwitchValue | |
strFlag = arrArguments(0) | |
Err.Clear() | |
'Help is needed | |
If (strFlag = "") OR (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _ | |
OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") OR (strFlag="h") Then | |
intParseCmdLine = CONST_SHOW_USAGE | |
Exit Function | |
End If | |
strOptionsMessage = strOptionsMessage & "" & VbCrLf | |
strOptionsMessage = strOptionsMessage & WScript.ScriptName & VbCrLf | |
strOptionsMessage = strOptionsMessage & "" & VbCrLf | |
strOptionsMessage = strOptionsMessage & "Command Line Options:" & vbCrLf | |
strOptionsMessage = strOptionsMessage & "=======================================" & VbCrLf | |
For i = 0 to UBound(arrArguments) | |
strFlag = Left(arrArguments(i), InStr(1, arrArguments(i), ":")-1) | |
If Err.Number Then 'An error occurs if there is no : in the string | |
Err.Clear | |
Select Case LCase(arrArguments(i)) | |
Case "/q" | |
blnQuiet = True | |
strOptionsMessage = strOptionsMessage & "Supress Console Output: " & blnQuiet & VbCrLf | |
Case "/taskbar" | |
blnTaskbar = True | |
strOptionsMessage = strOptionsMessage & "Pin to Taskbar instead of Start Menu: " & blnTaskbar & VbCrLf | |
Case Else | |
Wscript.Echo arrArguments(i) & " is not recognized as a valid input." | |
intParseCmdLine = CONST_ERROR | |
Exit Function | |
End Select | |
Else | |
strSwitchValue = Right(arrArguments(i), Len(arrArguments(i))-(Len(strFlag)+1)) | |
Select Case LCase(strFlag) | |
Case "/item" | |
strPath = strSwitchValue | |
strOptionsMessage = strOptionsMessage & "Item to pin to Start Menu or Taskbar: " & strPath & VbCrLf | |
Case else | |
Wscript.Echo "Invalid flag " & strFlag & "." | |
Wscript.Echo "Please check the input and try again." | |
intParseCmdLine = CONST_ERROR | |
Exit Function | |
End Select | |
End If | |
Next | |
If (strPath = "") Then | |
Wscript.Echo "The /item switch is required" | |
Wscript.Echo "Please check the input and try again." | |
intParseCmdLine = CONST_ERROR | |
Exit Function | |
End If | |
intParseCmdLine = CONST_PROCEED | |
End Function | |
'******************************************************************** | |
'* | |
'* Function PinItem() | |
'* | |
'* Purpose: Pin item to the Start Menu. | |
'* | |
'* Input: strlPath Path of exe to pin | |
'* blnTaskbar Pin item to Taskbar instead of Start Menu if true | |
'* | |
'* Dependencies: objShell Shell.Application object | |
'* objFSO File System object | |
'* | |
'* Returns: True if the shortcut is created, else false | |
'* | |
'******************************************************************** | |
Function PinItem(strlPath, blnTaskbar) | |
On Error Resume Next | |
Dim colVerbs | |
Dim itemverb | |
Dim objFolder | |
Dim objFolderItem | |
Dim strFolder | |
Dim strFile | |
If objFSO.FileExists(strlPath) Then | |
'***** Do nothing, folder exists | |
Else | |
'***** Folder does not exist | |
PinItem = False | |
WScript.Echo "File to pin does not exist." | |
WScript.Echo "Please check the input and try again." | |
Exit Function | |
End If | |
strFolder = objFSO.GetParentFolderName(strlPath) | |
strFile = objFSO.GetFileName(strlPath) | |
WScript.Echo "Folder: " & strFolder | |
WScript.Echo "File: " & strFile | |
Err.Clear | |
Set objFolder = objShell.Namespace(strFolder) | |
Set objFolderItem = objFolder.ParseName(strFile) | |
' ***** InvokeVerb for this does not work on Vista/WS2008 | |
'objFolderItem.InvokeVerb("P&in to Start Menu") | |
' ***** This code works on Vista/WS2008 | |
Set colVerbs = objFolderItem.Verbs | |
If blnTaskbar Then | |
For each itemverb in objFolderItem.verbs | |
If Replace(itemverb.name, "&", "") = "Pin to Taskbar" Then itemverb.DoIt | |
Next | |
Else | |
For each itemverb in objFolderItem.verbs | |
If Replace(itemverb.name, "&", "") = "Pin to Start Menu" Then itemverb.DoIt | |
Next | |
End If | |
If Err.Number = 0 Then | |
PinItem = True | |
Else | |
PinItem = False | |
End If | |
End Function | |
'******************************************************************** | |
'* | |
'* Sub ShowUsage() | |
'* | |
'* Purpose: Shows the correct usage to the user. | |
'* | |
'* Input: None | |
'* | |
'* Output: Help messages are displayed on screen. | |
'* | |
'******************************************************************** | |
Sub ShowUsage() | |
WScript.Echo "This script is used to Pin items to the Start Menu or Taskbar." | |
WScript.Echo "" | |
WScript.Echo "Usage: cscript " & WScript.ScriptName & " [options]" | |
WScript.Echo "" | |
WScript.Echo "Options:" | |
WScript.Echo "" | |
WScript.Echo " /item:<PathName> Path of item to pin." | |
WScript.Echo "" | |
WScript.Echo " /taskbar (Optional) Pin to Taskbar instead of Start Menu." | |
WScript.Echo "" | |
WScript.Echo " /? (Optional) Displays this help text." | |
WScript.Echo "" | |
WScript.Echo "" | |
WScript.Echo "" | |
End Sub |
I dont know if someone will see this. I hope so. I am running it as part of a script and I am copying a shortcut (*.lnk) file from my thumb drive. I am using the path of "%~0\prep\shortcut.lnk". It displays the following error:
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
PinItems.vbs
Command Line Options:
Item to pin to Start Menu or Taskbar: F:\itempin.bat\PREP\shortcut.lnk
File to pin does not exist.
Please check the input and try again.
Item pinned: False
Press any key to continue . . .
Any Ideas
I have a some what similar issue to the previous comment. I have a item with an ® in it that the script doesn't like. Ex. ("Name® Pro.lnk") and the script doesn't like it. If I rename the item and takeout the ® then it works. So how do get the script to allow this character.?
Thanks
Found my answer. It needed to be wrote like ("Name" & Chr(174) & " Pro.lnk")
so I tried it... the command log said it did it but it did not appear on my taskbar even after I restarted windows explorer
C:\Users\Mulverine\AppData\Roaming\Mulvpack>cscript shortcut.vbs /taskbar /item:"C:\Users\Mulverine\AppData\Roaming\Mulvpack\MulvPack.lnk"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
shortcut.vbs
Command Line Options:
Pin to Taskbar instead of Start Menu: True
Item to pin to Start Menu or Taskbar: C:\Users\Mulverine\AppData\Roaming\Mulvpack\MulvPack.lnk
Folder: C:\Users\Mulverine\AppData\Roaming\Mulvpack
File: MulvPack.lnk
Item pinned: True
I seriously don't know how Windows System Admins don't go postal and storm Redmond with pitchforks, reading a post like this. I'm trying to find a way to automate an installation of Windows Management workstations using Powershell in AWS, to install the main AD Tools icons on the taskbar. As someone coming from the linux world, I'd expect that to be as simple as:
sudo ln -s /usr/bin/program /home/default/bin/program
Seriously, one line. Maybe two if you need to set permissions on the result. Sudo is needed to write to a location normally writable only by root.
Instead, I've spent 2 hours, going through hundreds of posts, finally hit my limit on this one - some HUUUUUUGGGGGEEE script to do such a simple task, and - does this work in Windows 10, or 2008 or ... WTF? This is insane. This is a SIMPLE thing. It should have a SIMPLE way of doing it. The fact that people are not up in arms about the needless and useless complexity which is Windows just astounds me. I really don't know why anyone would build a new system on this OS without a gun to their head.
Ditto PowerShell, 10x the useless syntax complexity needed in any other scripting language. Who writes this stuff? Better yet, who chooses to use this, when it's so much simpler, not to mention far cheaper, to use bash/python on free linux.
Sorry, just had to vent. After all this, I'm not even going to try to automate installing icons in the task bar. It's just astoundingly complex, which means it's likely to be brittle and break often.
PinItem.vbs does not seem to work on server 2016. What has to be changed for it to work?
Hi, I am trying to use your code to place icons on a locked down end user account. I am doing this on Win7. I modified the CMD file to try creating few shortcuts on the start menu. It creates shortcuts of .exe files just fine, but when I tried to create a shortcut of another vbs file, it returns pinned = Ture, but does not actually create the shortcut of the file. Is that a code limitation in the PinItem.vbs?
Here is the CMD file modification:
@echo off
cscript PinItem.vbs /item:"C:\Program Files\Mozilla Firefox\Firefox.exe"
cscript PinItem.vbs /item:"E:\Users\AMaskara\My Documents\Service Folder\4X\Software\netscan.exe"
cscript PinItem.vbs /item:"E:\Users\AMaskara\My Documents\Service Folder\4X\BatchFiles\CleanupFTP\CleanupFTP.vbs"
echo.
echo ----------
REM echo Deleting Self...
REM del %0
pause