How to create a shortcut from the command line

Working on Windows, I install Windows a lot.  This means a lot of my customizations have to be re-applied every time I install.  To save myself some time I created a script which applies some of them. Last time I showed how to set the desktop wallpaper from a command-line app.

This time, a script to create a shortcut.  The example usage creates a shortcut to Notepad and puts that in the “SendTo” folder.  I find this very useful because I often need to edit text files that have non-“.txt” assocations.  (There are also other shortcuts I create with it.)

notepad-sendto

Here’s the script:

>type create-shortcut.vbs
If WScript.Arguments.Count < 2 Or WScript.Arguments.Count > 3 Then
    WScript.Echo "Expected two or three arguments; got " & WScript.Arguments.Count
    WScript.Echo "First argument is the file to create"
    WScript.Echo "Second is the command to link to"
    WScript.Echo "Third, if present, is the arguments to pass"
    WScript.Quit
End If

Set shell = WScript.CreateObject("WScript.Shell")

Set link = shell.CreateShortcut(WScript.Arguments(0))
link.TargetPath = WScript.Arguments(1)

If WScript.Arguments.Count = 3 Then
    link.Arguments = WScript.Arguments(2)
End If

link.Save
>cscript create-shortcut.vbs "%appdata%\Microsoft\Windows\SendTo\Notepad.lnk" notepad.exe

EDIT 2015-10-31: added script to https://github.com/mvaneerde/blog/blob/master/scripts/create-shortcut.vbs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s