Windows Script Host (WSH) is the way you run desktop VBScript applications. VBScript is a version of Visual Basic designed for administrators or anybody to write applications that run directly on the operating system of your PC. If you don't happen to have it, you can download it from Microsoft completely free.
This Quick Tip is about the WSH CreateShortCut object. An example coded by Microsoft is shown below. It's particularly nice because it's a complete application with a Welcome window and a way to cancel. So if you need any part of this, feel free to steal. (I did.)
Check it out and then we'll discuss the content.
L_Welcome_MsgBox_Message_Text = _
"A shortcut to Notepad" & _
vbcrlf & "will be created on your desktop."
L_Welcome_MsgBox_Title_Text = _
"Windows Scripting Host Sample"
Call Welcome()
Dim WSHShell
Set WSHShell = _
WScript.CreateObject("WScript.Shell")
Dim MyShortcut, MyDesktop, DesktopPath
' Read desktop path using WshSpecialFolders object
DesktopPath = _
WSHShell.SpecialFolders("Desktop")
' Create a shortcut object on the desktop
Set MyShortcut = _
WSHShell.CreateShortcut( _
DesktopPath & "\Shortcut to notepad.lnk")
' Set shortcut object properties and save it
MyShortcut.TargetPath = _
WSHShell.ExpandEnvironmentStrings( _
"%windir%\notepad.exe")
MyShortcut.WorkingDirectory = _
WSHShell.ExpandEnvironmentStrings( _
"%windir%")
MyShortcut.WindowStyle = 4
MyShortcut.IconLocation = _
WSHShell.ExpandEnvironmentStrings( _
"%windir%\notepad.exe, 0")
MyShortcut.Save
WScript.Echo _
"A shortcut to Notepad now exists on your Desktop."
Sub Welcome()
Dim intDoIt
intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, _
vbOKCancel + vbInformation, _
L_Welcome_MsgBox_Title_Text )
If intDoIt = vbCancel Then
WScript.Quit
End If
End Sub
The script creates the WshShell object because the CreateShortcut method requires it. According to Microsoft, "You create a WshShell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder. The WshShell object provides the Environment collection. This collection allows you to handle environmental variables (such as WINDIR, PATH, or PROMPT)."
Notice that Microsoft's example sets several properties:
- TargetPath
- WorkingDirectory
- WindowStyle
- IconLocation
In addition to these properties, you can also set:
Description
MyShortcut.Description = _
"Shortcut to my file"
HotKey
MyShortcut.HotKey = _
"CTRL+ALT+SHIFT+X"
Arguments - Any parameters to pass
MyShortcut.Arguments = _
"Whatever"
Finally, CreateShortcut has some built-in intelligence that creates a different type of shortcut depending on the extension you provide in your code. You can either create a shortcut to an Internet resource (a WshURLShortcut object) or a shortcut to a file system object on your PC or a network (a WshShortcut object). Which one depends on the file extension you code in the PathLink argument. If it's .lnk, CreateShortcut creates a WshShortcut object; if it's .url, then it creates a WshURLShortcut object. And anything else is an error.

