108

How can I create a shortcut file (.lnk) to another file or executable, using command line utilities?

12
  • 1
    There doesn't appear to be any straightforward way to do that. Some people have written tools that let you do it; here's one of them. A Google search for "windows create shortcut command line" turns up some others. (I haven't tried any of them.) Feb 20, 2012 at 18:51
  • @iglvzx - I'm not sure that the editing you did is correct. I don't think that Shantanu needs a batch script - it could be any way of creating a *.lnk to another *.exe file.
    – Nir Alfasi
    Feb 20, 2012 at 19:53
  • @alfasin I added (.ink file), as there was some confusion. I revised the question to reflect Shantanu's comment. While you do provide a way to make 'shortcuts', it does not answer this specific question.
    – iglvzx
    Feb 20, 2012 at 19:57
  • 2
    Use the following: powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Desktop\shortcut.lnk');$s.TargetPath='C:\Windows\';$s.Save()" Obviously, replace "'%userprofile%\Desktop\shortcut.lnk" and "C:\Windows\" with your shortcut path and target path, respectively.
    – cowlinator
    Oct 18, 2017 at 19:38
  • @cowlinator As typed, your suggestion does not work.
    – user494585
    Oct 23, 2017 at 21:22

10 Answers 10

73

There is some very useful information on this site: http://ss64.com/nt/shortcut.html

Seems like there is some shortcut.exe in some resource kit which I don't have.
As many other sites mention, there is no built-in way to do it from a batch file.

But you can do it from a VB script:

Optional sections in the VBscript below are commented out:

Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "C:\MyShortcut.LNK"
Set oLink = oWS.CreateShortcut(sLinkFile)
    oLink.TargetPath = "C:\Program Files\MyApp\MyProgram.EXE"
 '  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

So, if you really must do it, then you could make your batch file write the VB script to disk, invoke it and then remove it again. For example, like so:

@echo off
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "%HOMEDRIVE%%HOMEPATH%\Desktop\Hello.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:\Windows\notepad.exe" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs

Running the above script results in a new shortcut on my desktop:
Resulting shortcut

Here's a more complete snippet from an anonymous contributor (updated with a minor fix):

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET LinkName=Hello
SET Esc_LinkDest=%%HOMEDRIVE%%%%HOMEPATH%%\Desktop\!LinkName!.lnk
SET Esc_LinkTarget=%%SYSTEMROOT%%\notepad.exe
SET cSctVBS=CreateShortcut.vbs
SET LOG=".\%~N0_runtime.log"
((
  echo Set oWS = WScript.CreateObject^("WScript.Shell"^) 
  echo sLinkFile = oWS.ExpandEnvironmentStrings^("!Esc_LinkDest!"^)
  echo Set oLink = oWS.CreateShortcut^(sLinkFile^) 
  echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^("!Esc_LinkTarget!"^)
  echo oLink.Save
)1>!cSctVBS!
cscript //nologo .\!cSctVBS!
DEL !cSctVBS! /f /q
)1>>!LOG! 2>>&1
10
  • 1
    Related: MSDN, Shell Links
    – iglvzx
    Feb 20, 2012 at 20:11
  • This works great for shortcut to a file. However, i'm having a weird problem using it for shortcut to a folder, when my variable Esc_LinkTarget contains an environment variable trying to get its parent folder. (Something like %CD%\.. does not work, but %CD% works). The shortcut target type becomes a 'File' instead of 'Folder'
    – EDM
    Mar 18, 2016 at 1:37
  • 1
    @Edmund Interesting problem. I don't have time to look into it, but I would assume a trailing slash could make a difference. Mar 18, 2016 at 14:53
  • Note: if you use SET Esc_LinkTarget=%0 then you have to remove the " from echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^(!Esc_LinkTarget!^)
    – Black
    Jul 28, 2017 at 22:35
  • 2
    Instead of creating a vbscript for each execution it would have been far better to use Wscript.Arguments to get the command line arguments... lol
    – Sancarn
    May 8, 2019 at 10:29
42

Here's a similar solution using powershell (I know, you can probably re-write your whole batch file in PS, but if you just want to Get It Done™...)

set TARGET='D:\Temp'
set SHORTCUT='C:\Temp\test.lnk'
set PWS=powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile

%PWS% -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut(%SHORTCUT%); $S.TargetPath = %TARGET%; $S.Save()"

You may have to explicity specify the path to PS in your file, but it should work. There are some additional attributes you can mangle through this object, too:

Name             MemberType Definition                             
----             ---------- ----------                             
Load             Method     void Load (string)                     
Save             Method     void Save ()                           
Arguments        Property   string Arguments () {get} {set}        
Description      Property   string Description () {get} {set}      
FullName         Property   string FullName () {get}               
Hotkey           Property   string Hotkey () {get} {set}           
IconLocation     Property   string IconLocation () {get} {set}     
RelativePath     Property   string RelativePath () {set}           
TargetPath       Property   string TargetPath () {get} {set}       
WindowStyle      Property   int WindowStyle () {get} {set}         
WorkingDirectory Property   string WorkingDirectory () {get} {set} 
2
23

Besides shortcut.exe, you can also use the command line version of NirCmd to create shortcut. http://nircmd.nirsoft.net/shortcut.html

1
  • 17
    I recomend almost everything from NirSoft, it's the ultimate geek toolset Sep 30, 2013 at 23:29
21

How about using mklink command ? C:\Windows\System32>mklink Creates a symbolic link.

MKLINK [[/D] | [/H] | [/J]] Link Target

    /D      Creates a directory symbolic link.  Default is a file
            symbolic link.
    /H      Creates a hard link instead of a symbolic link.
    /J      Creates a Directory Junction.
    Link    specifies the new symbolic link name.
    Target  specifies the path (relative or absolute) that the new link
            refers to.
2
  • 13
    Good idea, but symlinks appear to behave a bit differently than shortcuts. If I create a shortcut to a Visual Studio solution, it opens all the relatively-pathed-projects correctly. However, if I open the same solution via a symlink, the working directory is that of the path in which the symlink resides, not the path to which it refers. Apr 25, 2014 at 16:54
  • 2
    Also creating simlink requires admin privilege. Apr 26, 2021 at 14:32
13

If you have Git installed, it comes bundled with create-shortcut.exe which allows you to create shortcuts from the command line, and works in Windows 10. This utility is not AFAICT publicly documented, and the --help is minimal:

Usage: create-shortcut.exe [options] <source> <destination>

However, using Sysinternals's strings utility to extract strings from the .exe, I was able to work out the [options] and the mappings to the fields shown in shortcuts' Properties page:

--work-dir ('Start in' field)
--arguments (tacked onto the end of the 'Target')
--show-cmd (I presume this is the 'Run' droplist, values 'Normal window', 'Minimised', 'Maximised')
--icon-file (allows specifying the path to an icon file for the shortcut)
--description ('Comment' field)

Example usage:

REM If bin folder already in your PATH, omit CD line:
cd /d "C:\Program Files\Git\mingw64\bin" 
create-shortcut.exe --work-dir "C:\path\to\files" --arguments "--myarg=myval" "C:\path\to\files\file.ext" "C:\path\to\shortcuts\shortcut.lnk"

The strings utility also reveals application compatibility with Windows Vista, 7, 8, 8.1 and 10.

For convenience, the bin folder can be added to your PATH as follows:

"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" "[Environment]::SetEnvironmentVariable(\"PATH\", \"$env:PATH;C:\Program Files\Git\mingw64\bin\", [System.EnvironmentVariableTarget]::User)"

(adjust C:\Program Files\Git\mingw64\bin if create-shortcut.exe exists in a different path on your system)

8
  • 1
    With the Git I have (verison 2.27.0.windows.1) I had to dig to find this file as it wasn't referenced in PATH. Found in C:\Program Files\Git\mingw64\bin Oct 1, 2020 at 17:30
  • @mattwilkie The path to the exe is indicated in 'Example usage' above, with a note about PATH
    – Jimadine
    Oct 2, 2020 at 11:16
  • Ahh, I see that now. I missed on first read because text field scrolled off right of screen (and my hasty reading). Oct 6, 2020 at 18:14
  • 1
    You dont need to install git. You can download standalone binary from github.com/git-for-windows/git-sdk-64/blob/main/mingw64/bin/…
    – hemn
    Mar 27, 2023 at 12:30
  • 1
    just use it under the bash.exe, which is in the git distribution as well. Then all PATH like issues are solved Jul 19, 2023 at 9:23
11

After all the discussions we had here, this is my suggested solution: download: http://optimumx.com/download/Shortcut.zip extract it on your desktop (for example). Now, suppose you want to create a shortcut for a file called scrum.pdf (also on desktop):
1. open CMD and go to desktop folder
2. run: Shortcut.exe /f:"%USERPROFILE%\Desktop\sc.lnk" /a:c /t:%USERPROFILE%\Desktop\scrum.pdf

it will create a shortcut called sc.lnk on your desktop that will point to the original file (scrum.pdf)

7
  • 1
    a shortcut is something you run from windows, since he used CMD in the title and put the tag "command-line" I assumed he wants to run it from CMD. A batch file is the equivalent of a windows "shortcut" when you run in CMD (dos like) env.
    – Nir Alfasi
    Feb 20, 2012 at 18:51
  • 2
    Since he put "shortcut (.lnk file)" in the body of the question, I assumed he wants to create an actual shortcut. Feb 20, 2012 at 18:52
  • 1
    sorry for clarity i wanted to have a icon on my desktop that i made in cmd that would be a shortcut to a exe file
    – Shantanu
    Feb 20, 2012 at 19:23
  • 1
    @twasbrillig works for me...
    – Nir Alfasi
    Oct 7, 2014 at 3:18
  • 1
    Works for Windows 11 Oct 13, 2022 at 21:08
2

You can create an easy .vbs script that is right for you:

Shortcut.vbs

If WScript.Arguments.Count <> 2 Then WScript.Quit 1
Set FSO = CreateObject("Scripting.FileSystemObject")
TargetPath = FSO.GetAbsolutePathName(WScript.Arguments(0))
WorkingDirectory = FSO.GetParentFolderName(TargetPath)
Set lnk = CreateObject("WScript.Shell").CreateShortcut(WScript.Arguments(1))
    lnk.TargetPath = TargetPath
    lnk.WorkingDirectory = WorkingDirectory
    lnk.Save

From command line or batch script enter this command:

wscript Shortcut.vbs file.txt file.lnk
1

This free program has required functionality http://www.nirsoft.net/utils/nircmd2.html: (sample from said web page) "Create a shortcut to Windows calculator under Start Menu->Programs->Calculators nircmd.exe shortcut "f:\winnt\system32\calc.exe" "~$folder.programs$\Calculators" "Windows Calculator"

My own sample to try: nircmd.exe shortcut "c:\windows\system32\calc.exe" "~$folder.desktop$" "Windows Calculator"

-1

I know this topic is old but I wanted to provide the simple solution that worked for me.

I first copied the .ico file to my C: drive. Then I created the shortcut on my desktop and set the icon to the ico file on my C: drive. I then copied both the .ico and shortcut to a network share that my users have access to. Once there I wrote the following batch file to copy the ico and .url to the users windows 7 desktop. This creates the shortcut on all users desktop and keeps the icon file I set when creating the shortcut. I hope this helps someone.

@echo off
Copy "\\sharename\folder\icon.ico" "C:\"
pause
copy "\\sharename\folder\shortcut.url" "C:\Users\All Users\Desktop"
pause
1
  • If this is the approach to take, it is better to create the actual shortcut (.lnk) which embeds the icon in it. That shortcut can then be copied everywhere.
    – LPChip
    Jul 2, 2016 at 9:13
-1

Step 1: Open CMD file location

enter image description here


Step 2: Right click properties on Command Prompt, and set favorite shortcut like this:

enter image description here

2
  • 3
    While I can understand the confusion, the question is about creating the "shortcut file" or "link file" (extension .lnk) of a shortcut itself. Your answer explains how to give an existing shortcut file a keyboard "shortcut". Your answer does not fit the question. But I will not downvote. It is well written and pictured and was given in good faith. I'd say it deserves an upvote just for the effort taken to compose it.
    – Antares
    May 1, 2020 at 10:13
  • 2
    Upvote given. At least, it does not deserve a negative score.
    – Antares
    May 1, 2020 at 10:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .