I'm working on a VBScript to create a shortcut link to a batch file (the batch file downloads and launches a frontend Access database copy). The following code technically works but the shortcut link it creates can't be pinned to the taskbar.
If I manually create a shortcut link with "cmd /c" followed by the filepath, the resultant shortcut can be pinned to the taskbar, for example.
Is it possible to create a shortcut in VBScript with "cmd /c", or something to that effect, so the link can be pinned to the taskbar?
Dim WSH, Link
Set WSH = Wscript.CreateObject("WScript.Shell")
userProfile = WSH.ExpandEnvironmentStrings("%userprofile%")
batchTargetPath = "Z:\Updater\MBL Database Updater.bat"
batchWorkingDirectory = "Z:\Updater\"
' Create a named shortcut object
Set Link = WSH.CreateShortcut(userProfile & "\MBL Database\MBL Database Updater Shortcut Test.lnk")
' Set the shortcut's properties and save it
Link.TargetPath = batchTargetPath
Link.WorkingDirectory = batchWorkingDirectory
Link.WindowStyle = 1
Link.IconLocation = userProfile & "\MBL Icons\MBL Updater.ico, 0"
Link.Save()
'Clear memory and finish up
Set WSH = Nothing
WScript.Quit()
edit: Link.TargetPath = "cmd /c " & Char(34) & batchTargetPath & Char(34)
and Link.TargetPath = "cmd /c ""Z:\Updater\MBL Database Updater.bat"""
result in "Invalid Procedure call or Argument" error.
Modify the VBScript file to:
Dim WSH, Link
Set WSH = Wscript.CreateObject("WScript.Shell")
userProfile = WSH.ExpandEnvironmentStrings("%userprofile%")
batchTargetPath = "Z:\Updater\MBL Database Updater.bat"
batchWorkingDirectory = "Z:\Updater"
' Create a named shortcut object
Set Link = WSH.CreateShortcut(userProfile & "\MBL Database\MBL Database Updater Shortcut Test.lnk")
' Set the shortcut's properties and save it
Link.TargetPath = WSH.Environment("PROCESS").Item("SystemRoot") & "\System32\cmd.exe"
Link.Arguments = "/D /C """ & batchTargetPath & """"
Link.WorkingDirectory = batchWorkingDirectory
Link.WindowStyle = 1
Link.IconLocation = userProfile & "\MBL Icons\MBL Updater.ico, 0"
Link.Save()
'Clear memory and finish up
Set WSH = Nothing
WScript.Quit()
That code creates the shortcut file with the property Target defined as:
C:\Windows\System32\cmd.exe /D /C "Z:\Updater\MBL Database Updater.bat"
The shortcut property Start in is defined by this script as Z:\Updater
with the directory separator \
at the end. That is the full path of the directory which should be the current working directory for the process created by Windows File Explorer on using the shortcut by calling the Windows kernel library function CreateProcess with a STARTUPINFO structure filled with data according to the shortcut properties. The directory path defined in the script file with batchWorkingDirectory
is passed to CreateProcess
with the function parameter lpCurrentDirectory
. The directory path should be without a trailing backslash although a directory path with a backslash at the end works too.
Link.TargetPath
must be just the fully qualified file name of the executable without its arguments. There should be executed a batch file which is a script file which needs a script interpreter for execution. The script interpreter is the Windows Command Processor with the fully qualified file name %SystemRoot%\System32\cmd.exe
. The script above gets the string value of the environment variable SystemRoot
and concatenates that string with operator &
with the string \System32\cmd.exe
. The result is the fully qualified file name of cmd.exe
with the correct directory path even on Windows is not installed to C:\Windows
as usual.
The arguments of the executable must be defined in the script with Link.Arguments
as one string. Please open a command prompt window, run cmd /?
and read the output usage help explaining the cmd
options /D
and /C
and how the command line after the option /C
or the option /K
is interpreted by cmd.exe
.
The first argument passed to cmd.exe
is /D
which disables the execution of the AutoRun
commands from registry under:
There is by Windows default no AutoRun
string value under both registry keys as the command line specified with the AutoRun
registry string value would be executed always first by cmd.exe
on not using option /D
on start of cmd.exe
. The Windows default for starting cmd.exe
is without using the option /D
. A shortcut or scheduled task starting cmd.exe
with option /D
is more secure and fail-safe. An AutoRun
string value added by a user/program/script does not change anything on using the option /D
on the execution of the specified batch file by a command line defined with an AutoRun
string value.
The second argument is the option /C
which instructs cmd.exe
to execute the command line specified with the next argument(s) and then close itself. The option /C
or the option /K
for execution of the command line and keep running must always be the last option to pass to cmd.exe
as all other argument string(s) are interpreted as command line to execute.
The third and last argument is in this case the fully qualified file name of the batch file to be executed by the Windows Command Processor which must be enclosed in "
because its fully qualified file name contains a space character.
A string is defined in VBScript syntax with "
at the beginning and "
at the end of the string. A double quote character being part of the string must be added to the string with escaping it with one more "
. The VBScript string "/D /C """
defines therefore the string /D /C "
. The first "
of """
is the escaping double quote to interpret the second "
literally as a character of the string while the third "
ends the string definition. """"
defines a string with the single character "
. The first "
begins the string definition. The second "
is the escaping double quote to interpret the third "
literally as a character of the string. The fourth "
ends the string definition.
See also the SS64 documentation for the VBScript command .CreateShortcut and the Microsoft page: How to create a desktop shortcut with the Windows Script Host?
The file extension of the batch file should be changed from .bat
to .cmd
as that is recommended for a batch file designed for an execution by cmd.exe
in comparison to a batch file with file extension .bat
designed for an execution by COMMAND.COM
of MS-DOS and Windows 95/98/ME. See also: Windows batch files: .bat vs .cmd?
The VBScript file could be also without the just once used variables batchTargetPath
and batchWorkingDirectory
which makes it easier seeing the strings being finally properties of the created shortcut in the script file:
Dim WSH, Link
Set WSH = Wscript.CreateObject("WScript.Shell")
userProfile = WSH.ExpandEnvironmentStrings("%userprofile%")
' Create a named shortcut object
Set Link = WSH.CreateShortcut(userProfile & "\MBL Database\MBL Database Updater Shortcut Test.lnk")
' Set the shortcut's properties and save it
Link.TargetPath = WSH.Environment("PROCESS").Item("SystemRoot") & "\System32\cmd.exe"
Link.Arguments = "/D /C ""Z:\Updater\MBL Database Updater.bat"""
Link.WorkingDirectory = "Z:\Updater"
Link.WindowStyle = 1
Link.IconLocation = userProfile & "\MBL Icons\MBL Updater.ico, 0"
Link.Save()
'Clear memory and finish up
Set WSH = Nothing
WScript.Quit()