How to create Shortcuts in Windows from the command line
December 3rd, 2008It seems that there is not a direct way to create a shortcut from the command line in Windows. The solution presented here takes less than two minutes to set up and works pretty good. However, if you find a better one, I would like to know.
First you have to create a small text file by the name mkshortcut.vbs. Use your favorite text editor to edit the file, even notepad will do. Then copy the following text and paste it into the file:
set WshShell = WScript.CreateObject("WScript.Shell" )
set oShellLink = WshShell.CreateShortcut(Wscript.Arguments.Named("shortcut") & ".lnk")
oShellLink.TargetPath = Wscript.Arguments.Named("target")
oShellLink.WindowStyle = 1
oShellLink.SaveThen save the file and exit the editor. Make sure that you move the file in a directory in your PATH (usually C:\WINDOWS\System32 is fine). Now, from the command line you can create shortcuts this way:
mkshortcut /target:TargetName /shortcut:ShortcutNameYou will have to replace TargetName with the name of the target file and ShortcutName with the name of the shortcut to be created (do not include a .lnk extension!). For example:
C:\>mkshortcut /target:"c:/documents and settings/giannis/desktop" /shortcut:"My Desktop"
C:\>dir *.lnk
Volume in drive C has no label.
Volume Serial Number is 70FC-EBB4
Directory of C:\
12/03/2008 11:12 AM 453 My Desktop.lnk
1 File(s) 453 bytes
0 Dir(s) 46,659,989,504 bytes free
C:\>Make sure that in target you include the full path of the target file name, starting with the drive letter and going down. For some reason Windows seem unable to create shortcuts with a relative path. Always use absolute paths for target.
August 2nd, 2011 at 2:17 am Just used this on Win7x64 - worked great. Thanks!
November 18th, 2011 at 11:30 am Good one. Helped me a lot
December 15th, 2011 at 5:37 pm Thanks for this tutorial, works great!
I'm wondering how I can add extra parameters for the shortcut, any ideas?
What I mean is something like this "C:\temp\file.exe -show edited" etc.
I tried using apostrophes but then it won't find the file at all!
December 15th, 2011 at 6:01 pm Got it.
If you need extra parameters, you'll have to add
oShellLink.Arguments = "-params /whatever"
after the TargetPath. :)
December 30th, 2011 at 9:46 am How would you set it to open with a program like textpad. Saying its like a config file
January 3rd, 2012 at 5:47 am Awesome script - is there any way to add arguments to set a specific icon file?
February 3rd, 2012 at 12:52 pm Good Work
May 27th, 2012 at 6:51 pm Awesome, thanks. Am using this in a project.
August 3rd, 2012 at 8:06 pm how you can specify a location to save the shortcut
we have target, name of the shortcut but if i want to save on the network drive (example "x:\DAY\voice.lnk")
we need a new option (example "/save")
C:\>mkshortcut /target:"c:/documents and settings/giannis/desktop" /shortcut:"My Desktop" /save:"x:\DAY"
September 28th, 2012 at 5:26 pm This command maybe help you
mklink /d %userprofile%\desktop\data d:\data
Good Luck
Iran ahwaz
October 16th, 2012 at 11:46 pm How can i add this to run on user logon, does the VBS script have to be on the local machine of the users?
October 18th, 2012 at 9:32 pm Can you please update this script to support generic shortcut creation?
mkshortcut.vbs /target:"" /shortcut: /location:
November 20th, 2012 at 5:47 pm Sweet! I used it succesfully to re-create a shortcut to Excel in my start menu. Doing it straight from the explorer gave me a nasty msvcr80.dll.
I rewrote the script a little to this:
set WshShell = WScript.CreateObject("WScript.Shell" )
sStartmenuPath = WshShell.SpecialFolders("StartMenu")
set oShellLink = WshShell.CreateShortcut(sStartmenuPath & "\" & Wscript.Arguments.Named("shortcut") & ".lnk")
oShellLink.TargetPath = Wscript.Arguments.Named("target")
oShellLink.WindowStyle = 1
oShellLink.Save
More info on SpecialFolders is here:
http://www.devguru.com/technologies/wsh/quickref/wshshell_specialfolders.html
Thanks for sharing!
January 18th, 2013 at 10:20 pm Just starting to learn scripting...
I think i have the basic language down but can some one post what this script would look like if it had a "location to save .lnk" object?
Im using this in a batch file process to copy files from a network drive then create a shortcut to delete the files when the user is done, need to copy the shortcut to the C:/User/Public/Desktop location.
Also when i run the mkshortcut.vbs from the command line i get this error:
Windows Script Host
Script: C:\Windows\system32\mkshortcut.vbs
Line:3
Char: 1
Error: Invalid proceedure call or argument
Code: 800A0005
Source: Microsoft VBScript runtime error
would appreciate any help.
January 18th, 2013 at 10:21 pm whoops i meant "location to save .lnk" argument!!!!!!
April 9th, 2013 at 2:21 pm Thanks. Great stuff.