TransWikia.com

How to make a shortcut from CMD?

Super User Asked on December 27, 2021

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

10 Answers

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

Answered by Mario Palumbo on December 27, 2021

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 FilesGitmingw64bin" 
create-shortcut.exe --work-dir "C:pathtofiles" --arguments "--myarg=myval" "C:pathtofilesfile.ext" "C:pathtoshortcutsshortcut.lnk"

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

Answered by Jimadine on December 27, 2021

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

Answered by gadolf on December 27, 2021

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 "\sharenamefoldericon.ico" "C:"
pause
copy "\sharenamefoldershortcut.url" "C:UsersAll UsersDesktop"
pause

Answered by Michele on December 27, 2021

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:winntsystem32calc.exe" "~$folder.programs$Calculators" "Windows Calculator"

My own sample to try: nircmd.exe shortcut "c:windowssystem32calc.exe" "~$folder.desktop$" "Windows Calculator"

Answered by goldie on December 27, 2021

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:Temptest.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} 

Answered by SmallClanger on December 27, 2021

How about using mklink command ? C:WindowsSystem32>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.

Answered by Mike on December 27, 2021

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

Answered by While Loop on December 27, 2021

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 FilesMyAppMyProgram.EXE"
 '  oLink.Arguments = ""
 '  oLink.Description = "MyProgram"   
 '  oLink.HotKey = "ALT+CTRL+F"
 '  oLink.IconLocation = "C:Program FilesMyAppMyProgram.EXE, 2"
 '  oLink.WindowStyle = "1"   
 '  oLink.WorkingDirectory = "C:Program FilesMyApp"
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%DesktopHello.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:Windowsnotepad.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

Answered by Der Hochstapler on December 27, 2021

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%Desktopsc.lnk" /a:c /t:%USERPROFILE%Desktopscrum.pdf

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

Answered by Nir Alfasi on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP