Create a Windows shortcut through a batch file (.bat)

22,341

Solution 1

Using batch alone? Probably not, unless you're just copying a shortcut from the Start Menu to somewhere else. We had this issue when building/refining our latest deployment process and certain groups wanted shortcuts to everything on their desktop.

The Windows NT Resource Kit has a utility named shortcut.exe that could do this. I have never tested it on XP/2003/2008.

If you can get away with VBS, this would work:

Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "C:\MyShortcut.LNK"
Set oLink = oWS.CreateShortcut(sLinkFile)

oLink.TargetPath = "C:\Program Files\MyApp\MyProgram.EXE"
' optional shortcut properties
' oLink.Arguments = ""
' oLink.Description = "MyProgram"
' oLink.HotKey = "ALT+CTRL+F"
' oLink.IconLocation = "C:\Program Files\MyApp\MyProgram.EXE, 2"
' oLink.WindowStyle = "1"
' oLink.WorkingDirectory = "C:\Program Files\MyApp"
oLink.Save

Source

Solution 2

Yes it is possible to create a shortcut, sort of... you can create a .url file which works just like a shortcut (.lnk) file from at least windows 98 on up to currently Windows 7. The .url file which is actually used for the Windows Favorites bookmarks is a simple text file which references a url and some additional information. Here is a simple example of making one from a batch file:

@echo off
echo [InternetShortcut] >> Explorer.url
echo URL=C:\WINDOWS\EXPLORER.EXE >> Explorer.url
echo IconFile=C:\WINDOWS\EXPLORER.EXE >> Explorer.url
echo IconIndex=0 >> Explorer.url

Which results in a file named Explorer.url to be created with this content inside of it:

[InternetShortcut]
URL=C:\WINDOWS\EXPLORER.EXE
IconFile=C:\WINDOWS\EXPLORER.EXE
IconIndex=0

Double clicking on it will work just as a shortcut and run the program.

Share:
22,341

Related videos on Youtube

Luca Matteis
Author by

Luca Matteis

http://scholar.google.com/citations?user=4shOPsgAAAAJ&hl=en

Updated on September 17, 2022

Comments

  • Luca Matteis
    Luca Matteis over 1 year

    Is it possible to create a shortcut from a .exe using batch scripting?

  • John Gardeniers
    John Gardeniers over 13 years
    That will only work reliably as you describe if IE is set as the default application to open a URL, which should never be assumed.
  • Garrett R. Hylltun
    Garrett R. Hylltun over 13 years
    Works regardless of whether or not IE is set as the default application to open a URL. I have used this form of shortcut since Windows 98 and have never had IE set as the default application to open a URL.
  • Garrett R. Hylltun
    Garrett R. Hylltun over 13 years
    If you notice, the association for .url in the registry points to rundll with and IE component as a parameter to the rundll. So, it's independent of whether IE is set to use .url files, but does require IE to be part of the system, which pretty much covers over 99%(uneducated estimate) of all Windows based PCs. Point is, likely only very deep computer users might be running without IE installed as part of their system and possibly some people in Europe who do not select IE as their browser when installing Windows since the mandatory selection screen.