Using a VBS Script to Create a Shortcut and a Shorcut to a URL

The below will show you how to use a VBS Script to Create a Shortcut and also a URL shortcut to a webpage.

In the below example script I am simply creating a shortcut to Notepad using the properties .TargetPath and . Description however there are a few other properties you can make use of.

 

Property Description
Arguments Additional command-line arguments that can be used when starting the application.
Description Description given to the shortcut.
FullName Read-only property that returns the complete path to the target application.
HotKey Keyboard shortcut: a combination of keys that, when held down together, will start the application. Keyboard shortcuts typically consist of one of the following keys plus a letter (az), number (09), or function key (F1F12):

  • ALT
  • CTRL
  • SHIFT

For example, to set the keyboard shortcut to the CTRL key and the 9 key, use this value:

CTRL + 9

If the key combination you select is already in use, it will be overwritten and will be applied to the new shortcut created by your script.

IconLocation Allows you to specify an icon and an icon index for the shortcut. If no location is specified, the default icon for the application is used.
TargetPath Complete path to the target application. You must specify the full path, including the drive letter or UNC path.

When setting a TargetPath, WSH will accept the value entered. It will not check to ensure that the path is correct.

WindowStyle Specifies the initial window type for the application. Valid styles are the same as those shown for the Run method and are listed in Table 3.9.
WorkingDirectory Specifies the working directory for the application.

 

 Example 1:

Set objShell = WScript.CreateObject("WScript.Shell")
 
'All users Desktop
allUsersDesktop = objShell.SpecialFolders("AllUsersDesktop")
 
'The current users Desktop
usersDesktop = objShell.SpecialFolders("Desktop")
 
'Where to create the new shorcut
Set objShortCut = objShell.CreateShortcut(usersDesktop & "\Notepad.lnk")
 
'What does the shortcut point to
objShortCut.TargetPath = "%SystemRoot%\system32\notepad.exe"
 
'Add a description
objShortCut.Description = "Run the Notepad."
 
'Create the shortcut
objShortCut.Save

 

Creating a URL Shortcut

URL shortcuts are created in the same way however they do not support all of the properties or a Standard shortcut, the file name must also be a .url not a .lnk

Example 2:

Set objShell = WScript.CreateObject("WScript.Shell")
 
'All users Desktop
allUsersDesktop = objShell.SpecialFolders("AllUsersDesktop")
 
'The current users Desktop
usersDesktop = objShell.SpecialFolders("Desktop")
 
'Where to create the new shorcut
Set objShortCut = objShell.CreateShortcut(usersDesktop & "\Geeks Hangout.url")
 
'What does the shortcut point to
objShortCut.TargetPath = "https://geekshangout.com"
 
'Create the shortcut
objShortCut.Save

 

 

 

2 thoughts on “Using a VBS Script to Create a Shortcut and a Shorcut to a URL”

  1. These work great and I”m currently using AllUsersDesktop to add a shortcut and icon to user desktops but this adds it to the Public Desktop folder in Windows 7 which means users can delete it. Is it possible to add the icon and shortcut to the Default Desktop Folder? (C:\Users\Default\Desktop)

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.