How to make a shortcut from CMD?

81

33

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

Shantanu

Posted 2012-02-20T18:24:10.937

Reputation: 1 085

2Use 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 – 2017-10-18T19:38:46.210

@cowlinator As typed, your suggestion does not work. – Ploni – 2017-10-23T21:22:49.497

@Ploni, it does work for me on my computer. What error message are you seeing? What is your powershell execution policy? – cowlinator – 2017-10-24T21:06:08.753

@cowlinator Strange. Now it works for me too. – Ploni – 2017-10-24T21:12:21.667

That's good to hear, if a bit mystifying. If anybody else is having issues, you can try typing the following directly into powershell: $s=(New-Object -COM WScript.Shell).CreateShortcut($Env:userprofile + '\Desktop\shortcut.lnk');$s.TargetPath='C:\Windows';$s.Save() – cowlinator – 2017-10-24T21:14:56.540

@cowlinator your comment contains multiple zero-width / non-printable characters. As you are asking users to copy and paste this into their command line, this can look quite bad from a security perspective. Please remove them and format your comment as raw string. – hyperknot – 2018-01-27T00:38:54.347

For anyone interested, here is a cleaned version of @cowlinator's comment: powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Desktop\shortcut.lnk');$s.TargetPath='C:\Windows\';$s.Save()" – hyperknot – 2018-01-27T00:42:25.843

@zsero, I was concerned about your comment, so I copy/pasted both of my above comments into notepad++ and selected "show all characters", but I found no non-printable characters. Also, I'd be pretty pretty surprised if stack-exchange sites didn't sanitize their inputs. What makes you believe there are non-printable characters there? – cowlinator – 2018-01-27T03:50:38.700

@cowlinator in Chrome, right click: Inspect on your text to see it has 3 occurances of ‌​ in it. – hyperknot – 2018-01-27T11:57:51.497

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.)

– Keith Thompson – 2012-02-20T18:51:18.690

@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. – alfasin – 2012-02-20T19:53:16.230

@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 – 2012-02-20T19:57:55.613

Answers

56

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

Der Hochstapler

Posted 2012-02-20T18:24:10.937

Reputation: 77 228

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 – 2016-03-18T01:37:08.050

1@Edmund Interesting problem. I don't have time to look into it, but I would assume a trailing slash could make a difference. – Der Hochstapler – 2016-03-18T14:53:50.033

Note: if you use SET Esc_LinkTarget=%0 then you have to remove the " from echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^(!Esc_LinkTarget!^) – Black – 2017-07-28T22:35:17.137

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 – 2019-05-08T10:29:39.650

Works like a charm. I liked the "Not complete snippet" more lol – GabrielBB – 2019-06-08T23:50:53.510

thank you so much!! – Egon Stetmann. – 2019-07-12T01:12:14.130

1

Related: MSDN, Shell Links

– iglvzx – 2012-02-20T20:11:07.173

24

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} 

SmallClanger

Posted 2012-02-20T18:24:10.937

Reputation: 341

Trix: If you believe that you have a better way of doing this, just post it as a new answer (linking to this one, if appropriate). – Scott – 2017-11-04T01:49:50.790

17

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

While Loop

Posted 2012-02-20T18:24:10.937

Reputation: 171

11I recomend almost everything from NirSoft, it's the ultimate geek toolset – That Brazilian Guy – 2013-09-30T23:29:32.043

12

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.

Mike

Posted 2012-02-20T18:24:10.937

Reputation: 137

10Good 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. – Walter Stabosz – 2014-04-25T16:54:40.220

7

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)

alfasin

Posted 2012-02-20T18:24:10.937

Reputation: 1 298

@twasbrillig works for me... – alfasin – 2014-10-07T03:18:02.597

That's not a shortcut; it's just a batch file that invokes a specified program. – Keith Thompson – 2012-02-20T18:47:43.437

1a 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. – alfasin – 2012-02-20T18:51:25.407

2Since he put "shortcut (.lnk file)" in the body of the question, I assumed he wants to create an actual shortcut. – Keith Thompson – 2012-02-20T18:52:47.440

1sorry 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 – 2012-02-20T19:23:56.907

now that I finally understood (slow thinker - what can you do...) I changed my answer. hope it helps! – alfasin – 2012-02-21T01:42:00.217

0

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

gadolf

Posted 2012-02-20T18:24:10.937

Reputation: 133

0

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"

goldie

Posted 2012-02-20T18:24:10.937

Reputation: 1

0

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

Michele

Posted 2012-02-20T18:24:10.937

Reputation: 11

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 – 2016-07-02T09:13:12.830