How to create a shortcut using a batch script?

263,179

Solution 1

You can achieve without external tools this by creating a temporary VBScript:

@echo off

set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\Desktop\myshortcut.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "D:\myfile.extension" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%

cscript /nologo %SCRIPT%
del %SCRIPT%

(Idea taken from here.)

This will create myshortcut.lnk on the Desktop, pointing to D:\myfile.extension.

You can supply additional properties before saving the link by modifying the following values:

oLink.Arguments
oLink.Description
oLink.HotKey
oLink.IconLocation
oLink.WindowStyle
oLink.WorkingDirectory

Consult How to create a desktop shortcut with the Windows Script Host to see a few examples.

Solution 2

@echo off
echo [InternetShortcut] >> "%AllUsersProfile%\desktop\NOTEPAD.url"
echo URL="C:\WINDOWS\NOTEPAD.EXE" >> "%AllUsersProfile%\desktop\NOTEPAD.url"
echo IconFile=C:\WINDOWS\system32\SHELL32.dll >> "%AllUsersProfile%\desktop\NOTEPAD.url"
echo IconIndex=20 >> "%AllUsersProfile%\desktop\NOTEPAD.url"

This code creates a shortcut in the "All Users" desktop folder called NOTEPAD.url pointing to the NotePad application, and will also assign an icon from the SHELL32.dll. Change the path and filename to your D:/ location and exename. And make sure your .url filename stays the same across all lines of code.

Solution 3

there is external command shortcut.exe that can do this in that way:

shortcut /a:c /f:"c:\users\me\desktop\myshortcut.lnk" /t:"c:\program files\skype\skype.exe"

that can create shortcut of skype in your desktop

it is free downloadable program, but i can't find its link, so i will try to upload it and post the link


here it is:

shortcut.exe by Marty List

Share:
263,179

Related videos on Youtube

ebinpaulose
Author by

ebinpaulose

Updated on September 18, 2022

Comments

  • ebinpaulose
    ebinpaulose over 1 year

    How can I create a shortcut to the file D:\myfile.extension on the Desktop using a batch script?

    • Admin
      Admin almost 2 years
      See also this answer I just posted, which calls a line of powershell from a batch script.
  • adventurer
    adventurer almost 12 years
    no, i didn't know that there is utility for it in resource kit by microsoft. i am currenlty using its alternative by optimum x
  • Dennis
    Dennis almost 12 years
    Nice idea. As long as you don't have to supply any arguments, this works well.
  • Logman
    Logman almost 10 years
    who down voted my answer? It does exactly what the poster wanted...
  • Dennis
    Dennis almost 10 years
    Somebody downvoted the question and all the answers (link).
  • industryworker3595112
    industryworker3595112 over 7 years
    Is it impossible to do with batch script? (apart from generating your proposed vbs and executing it)
  • JacobTheDev
    JacobTheDev almost 6 years
    Is there a way to set AppUserModelId using this method?
  • RoG
    RoG about 3 years
    Note that this can be modified to use relative paths (quite likely if this is being done via a script). The sLinkFile can be a relative path (e.g. just providing "myshortcut.lnk" puts it in the current directory). The target file can be set relative to the current directory by specifying oLink.TargetPath = "%cd%\folder\target.exe".
  • user2956477
    user2956477 over 2 years
    Notify, that name of Desktop folder is language dependent.