How do I create a shortcut via command-line in Windows?

216,108

Solution 1

You could use a PowerShell command. Stick this in your batch script and it'll create a shortcut to %~f0 in %userprofile%\Start Menu\Programs\Startup:

powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Start Menu\Programs\Startup\%~n0.lnk');$s.TargetPath='%~f0';$s.Save()"

If you prefer not to use PowerShell, you could use mklink to make a symbolic link. Syntax:

mklink saveShortcutAs targetOfShortcut

See mklink /? in a console window for full syntax, and this web page for further information.

In your batch script, do:

mklink "%userprofile%\Start Menu\Programs\Startup\%~nx0" "%~f0"

The shortcut created isn't a traditional .lnk file, but it should work the same nevertheless. Be advised that this will only work if the .bat file is run from the same drive as your startup folder. Also, apparently admin rights are required to create symbolic links.

Solution 2

Cannot be done with pure batch.Check the shortcutJS.bat - it is a jscript/bat hybrid and should be used with .bat extension:

call shortcutJS.bat -linkfile "%~n0.lnk" -target  "%~f0" -linkarguments "some arguments"

With -help you can check the other options (you can set icon , admin permissions and etc.)

Solution 3

Rohit Sahu's answer worked best for me in Windows 10. The PowerShell solution ran, but no shortcut appeared. The JScript solution gave me syntax errors. I didn't try mklink, since I didn't want to mess with permissions.

I wanted the shortcut to appear on the desktop. But I also needed to set the icon, the description, and the working directory. Note that MyApp48.bmp is a 48x48 pixel image. Here's my mod of Rohit's solution:

@echo off
cd c:\MyApp
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "%userprofile%\Desktop\MyApp.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:\MyApp\MyApp.bat" >> CreateShortcut.vbs
echo oLink.WorkingDirectory = "C:\MyApp" >> CreateShortcut.vbs
echo oLink.Description = "My Application" >> CreateShortcut.vbs
echo oLink.IconLocation = "C:\MyApp\MyApp48.bmp" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs

Solution 4

The best way is to run this batch file. open notepad and type:-

@echo off
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "GIVETHEPATHOFLINK.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "GIVETHEPATHOFTARGETFILEYOUWANTTHESHORTCUT" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs

Save as filename.bat(be careful while saving select all file types) worked well in win XP.

Solution 5

Nirsoft's NirCMD can create shortcuts from a command line, too. (Along with a pile of other functions.) Free and available here:

http://www.nirsoft.net/utils/nircmd.html

Full instructions here: http://www.nirsoft.net/utils/nircmd2.html#using (Scroll down to the "shortcut" section.)

Yes, using nircmd does mean you are using another 3rd-party .exe, but it can do some functions not in (most of) the above solutions (e.g., pick a icon # in a dll with multiple icons, assign a hot-key, and set the shortcut target to be minimized or maximized).

Though it appears that the shortcutjs.bat solution above can do most of that, too, but you'll need to dig more to find how to properly assign those settings. Nircmd is probably simpler.

Share:
216,108
Maslor
Author by

Maslor

Computer Networks Engineering Student.

Updated on July 10, 2022

Comments

  • Maslor
    Maslor almost 2 years

    I want my .bat script (test.bat) to create a shortcut to itself so that I can copy it to my windows 8 Startup folder.

    I have written this line of code to copy the file but I haven't yet found a way to create the said shortcut, as you can see it only copies the script.

    xcopy "C:\Users\Gabriel\Desktop\test.bat" "C:\Users\Gabriel\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
    

    Can you help me out?

  • npocmaka
    npocmaka about 9 years
    aah.+1. Forgot about mklink. Though it is supported on vista and above (anyway XP is on the way to the graveyard) and is not so rich on options. Powershell solution is much better :)
  • rojo
    rojo about 9 years
    Yeah, but powershell will cause a second's pause if it hasn't been primed for the current Windows session yet. Your shortcutJS.bat solution should be faster.
  • Maslor
    Maslor about 9 years
    I think that the mklink solution enough for the current project I'm doing, I don't see the need for a better yet more complex command. But there's something I need to bypass: admin rights. Is there an option to configure user rights in mklink?
  • rojo
    rojo about 9 years
    Using group policy editor you can modify Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment according to this page. There's also a way you can have your script prompt for UAC using a chunk of VB Script. Honestly, I think the PowerShell command or npocmaka's shortcutJS.bat solution will result in the best user experience.
  • Eryk Sun
    Eryk Sun about 9 years
    A filesystem symbolic link is fine for a file that will be run/opened via ShellExecuteEx or a bat/cmd script for which CreateProcess hard codes running the %ComSpec% command interpreter. But it won't work for an .exe (specifically a PE executable, regardless of extension) that looks for resources relative to the application directory. Thus unless the link is created in the same directory (e.g. prog.2.0.exe => prog.exe), a .lnk shortcut is generally the better solution.
  • Maslor
    Maslor about 9 years
    I'd like it to run without "asking for permission" so I was looking for a way to define rights in the batch itself
  • Maslor
    Maslor over 8 years
    I wanted a very simple solution to a very simple problem and I have already been given it. I was trying to just use BAT, not visual basic.
  • Maslor
    Maslor over 8 years
    But thanks for the answer! Might help someone else that comes here looking for solutions.
  • lengxuehx
    lengxuehx almost 8 years
    Great, it works very well. I think It's better than mklink method. I use shortcutJs.bat to create a shortcut for a .pyw file, the shortcut works exactly the same as the one created manually. But when you double click the link created by mklink, it can't auto find pythonw.exe to execute the .pyw file.
  • Andreas
    Andreas about 7 years
    @rojo Very good! Now I can use this in a batch file stored in my shell:sendto folder so that it will create shortcuts in my favourites folder. Just a small correction: I think it should be %~nx1 and %~f1, since %~0 is the name of the batch script itself and not the file that gets passed to the batch file.
  • Rodrigo V
    Rodrigo V almost 7 years
    I had problems getting the Desktop path in a Terminal Server where users were using custom Desktop location. To solve this, I had to read the Desktop location from the registry. Set objShell = CreateObject("WScript.Shell") strRegDesktopPath = objShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windo‌​ws\CurrentVersion\Ex‌​plorer\User Shell Folders\Desktop") desktopPath = objShell.ExpandEnvironmentStrings(strRegDesktopPath) Set oWS = WScript.CreateObject("WScript.Shell") sLinkFile = desktopPath & "\MyApp.lnk"
  • philwalk
    philwalk about 6 years
    worked for me, although I converted it to the equivalent cygwin shell script first.
  • MDuh
    MDuh almost 5 years
    I'm having problems adding a double quote inside the linkarguments, how do you add a double quote in the args?
  • npocmaka
    npocmaka almost 5 years
    @MDuh - I'll have to update the script in order to support this. Probably during the weekend.
  • Saran
    Saran about 4 years
    mklink (without switches) creates a symbolic link, not a .lnk shortcut
  • Cromax
    Cromax about 4 years
    Yet, when one uses IconFile, then IconIndex is mandatory, even if icon file is an *.ico file (index should be 0 then, just as in the example).
  • Riccardo Bassilichi
    Riccardo Bassilichi over 3 years
    the powershell version no need UAC... it is pervect! Thank you!
  • Bilbo
    Bilbo over 3 years
    Under Win 8.1N at least, the last line should be "echo IconIndex=^0>> %SHRT_LOCA%". Without that, the line ends up in the console, not in the file, because Cmd.EXE treats "0>>" as redirecting the echo to StdOut. Handle Zero is StdIn and StdOut is handle One, but the "^" escapes the redirection regardless.