How to Create a ShortCut using Scripting on Windows ?


The shortcut on the windows platform is a link to the existing programs. For example, you can right click the desktop and follow the wizard to create a shortcut and place it on your desktop. To do this in the scripting is useful especially if you want to do some administrative tasks automatically.

The WSH (Windows Script Hosting) is available almost on all windows versions. Therefore, you could use the inbuilt scripting languages: JScript (Microsoft version of Javascript) or VBScript to program the scripting.

VBScript

Save the following into *.vbs and double click that will invoke cscript.exe (or wscript.exe) to run the scripts. The short cut will be placed on Desktop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 & "\notepad shortcut.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 "Shortcut has been placed on desktop"
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 & "\notepad shortcut.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 "Shortcut has been placed on desktop"

JScript

The JScript version is similar except that you need to save the following code into *.js.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var WSHShell = WScript.CreateObject("WScript.Shell");
 
// Read desktop path using WshSpecialFolders object
var DesktopPath = WSHShell.SpecialFolders("Desktop");
 
// Create a shortcut object on the desktop
var MyShortcut = WSHShell.CreateShortcut(DesktopPath + "\notepad shortcut.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("Notepad shortcut has been placed on desktop");
var WSHShell = WScript.CreateObject("WScript.Shell");

// Read desktop path using WshSpecialFolders object
var DesktopPath = WSHShell.SpecialFolders("Desktop");

// Create a shortcut object on the desktop
var MyShortcut = WSHShell.CreateShortcut(DesktopPath + "\notepad shortcut.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("Notepad shortcut has been placed on desktop");

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
340 words
Last Post: How to Plot 3D Line Vectors in Matlab Using quiver3 ?
Next Post: Spot a dramatic increase of CPC by blocking low CPC URLs in Adsense

The Permanent URL is: How to Create a ShortCut using Scripting on Windows ?

Leave a Reply