Peter Hinchley

How to Create a Shortcut Using VBScript

Tagged: windows, vbscript

I occasionally need to programmatically create shortcuts. This can be achieved with VBScript using the CreateShortcut method of the WshShell object.

The following code creates a shortcut called Notepad on the public desktop (shared by all users) of a Windows Vista/7 computer. The name of the shortcut is defined by the argument to the CreateShortcut method, and the path to the shortcut executable is defined by TargetPath (the other properties are optional). The Save method is used to commit the setting and create the shortcut.

Set oShell = CreateObject("WScript.Shell")
Set oShortcut = oShell.CreateShortcut("C:\Users\Public\Desktop\Notepad.lnk")

oShortcut.TargetPath = "C:\Windows\Notepad.exe"
oShortcut.WorkingDirectory = "C:\Windows"
oShortcut.Description = "Notepad"
oShortcut.Hotkey = "CTRL+SHIFT+N"
oShortcut.Save

To use the script, save the code into a file named C:\Temp\makeshortcut.vbs. Open a command prompt, navigate to C:\Temp, and enter the following command:

cscript.exe makeshortcut.vbs

The Mob Hath Spoken

Dan:

I get an error on the save. Here is my code:

Set oShell = CreateObject("WScript.Shell")
Set oShortcut = oShell.CreateShortcut("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\TESTIFOEdit.lnk")
oShortcut.TargetPath = "D:\SOFTWARE ARCHIVE\Windows\Disk & File Management\IFOEdit\IfoEdit.exe"
oShortcut.WorkingDirectory = "D:\SOFTWARE ARCHIVE\Windows\Disk & File Management\IFOEdit"
oShortcut.Description = "IFOEdit"
' oShortcut.Hotkey = "CTRL+SHIFT+N"
oShortcut.IconLocation = "D:\SOFTWARE ARCHIVE\Windows\Disk & File Management\IFOEdit\IfoEdit.exe"
oShortcut.Save

Any ideas?

Hinch:

The code looks good. The only reason I can see for it failing is either the file and folder paths specified don't exist, or the file system permissions prevent you from creating a shortcut in the Start Menu. Are you running the script as an administrator?

Your Say