How to Create a Shortcut (.LNK) File in Batch Script
Creating desktop or Start Menu shortcuts is a standard part of software installation and user environment setup. Unlike simple file operations, batch scripting has no native, built-in command to create a Windows shortcut (.lnk file). These are complex binary files, not simple text pointers, and require a more advanced tool to generate.
This guide will teach you the standard and most reliable methods for creating shortcuts from a batch script by using helper scripting languages. You will learn the classic, highly-compatible method of generating a temporary VBScript file, and the modern, cleaner approach using a PowerShell one-liner.
The Challenge: No Native Command (and Why MKLINK Isn't the Answer)
It is critical to understand that the MKLINK command does not create .lnk shortcuts. MKLINK is used for creating symbolic links and hard links, which are file-system-level pointers invisible to most users. A shortcut, on the other hand, is a user-facing file that can have its own icon, arguments, and other properties.
To create a shortcut, we must use the Windows Script Host (WSH) object model, which is accessible through languages like VBScript or PowerShell.
The Classic Method: Generating a Temporary VBScript
This method is the most compatible, working on virtually all Windows versions (from XP onwards). The batch script itself writes a small VBScript file (.vbs) to disk, executes it, and then deletes it. This makes the entire process self-contained within a single batch file.
This script creates a simple shortcut on the desktop.
@ECHO OFF
SET "TARGET_EXE=C:\Windows\System32\notepad.exe"
SET "SHORTCUT_NAME=%USERPROFILE%\Desktop\My Notepad.lnk"
SET "VBS_FILE=%TEMP%\CreateShortcut.vbs"
ECHO Set oWS = WScript.CreateObject("WScript.Shell") > "%VBS_FILE%"
ECHO sLinkFile = "%SHORTCUT_NAME%" >> "%VBS_FILE%"
ECHO Set oLink = oWS.CreateShortcut(sLinkFile) >> "%VBS_FILE%"
ECHO oLink.TargetPath = "%TARGET_EXE%" >> "%VBS_FILE%"
ECHO oLink.Save >> "%VBS_FILE%"
ECHO Creating shortcut...
cscript //nologo "%VBS_FILE%"
DEL "%VBS_FILE%"
ECHO Done.
The Modern Method (Recommended): Using PowerShell
For any modern Windows system (Windows 7 and newer), calling PowerShell is a cleaner and more direct approach. It accesses the same underlying Windows components but avoids creating a temporary file, making your script more efficient.
This PowerShell one-liner, called from a batch file, achieves the same result.
@ECHO OFF
SET "TARGET_EXE=C:\Windows\System32\notepad.exe"
SET "SHORTCUT_NAME=%USERPROFILE%\Desktop\My Notepad.lnk"
ECHO Creating shortcut with PowerShell...
powershell -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut('%SHORTCUT_NAME%'); $s.TargetPath = '%TARGET_EXE%'; $s.Save()"
ECHO Done.
This is the recommended method for its conciseness and for avoiding temporary files.
Customizing the Shortcut (Icon, Arguments, Working Directory)
A basic shortcut is useful, but you often need to set additional properties. Both VBScript and PowerShell can easily modify these.
Key Shortcut Properties:
TargetPath: The executable or file the shortcut points to.Arguments: Command-line arguments to pass to the target.WorkingDirectory: The directory where the executable will start.IconLocation: The path to a file containing the icon (e.g., an.exeor.icofile).WindowStyle: How the window should open (e.g., 1=Normal, 3=Maximized, 7=Minimized).
The following is a PowerShell example with more options:
@ECHO OFF
SET "TARGET_EXE=C:\Program Files\MyApp\app.exe"
SET "SHORTCUT_NAME=%USERPROFILE%\Desktop\My App.lnk"
SET "ARGUMENTS=--user-mode --config=prod"
SET "WORKING_DIR=C:\Program Files\MyApp"
SET "ICON_PATH=C:\Program Files\MyApp\app.ico"
powershell -Command ^
"$ws = New-Object -ComObject WScript.Shell; " ^
"$s = $ws.CreateShortcut('%SHORTCUT_NAME%'); " ^
"$s.TargetPath = '%TARGET_EXE%'; " ^
"$s.Arguments = '%ARGUMENTS%'; " ^
"$s.WorkingDirectory = '%WORKING_DIR%'; " ^
"$s.IconLocation = '%ICON_PATH%'; " ^
"$s.Save()"
ECHO Customized shortcut created.
The caret (^) is used to break the long PowerShell command across multiple lines for readability.
Common Pitfalls and How to Solve Them
Problem: The Script is Not Run as Administrator
If you try to create a shortcut in a protected location, such as the Public Desktop (C:\Users\Public\Desktop) or the All Users Start Menu, the script will fail with an "Access is denied" error.
Solution: Run as Administrator
Any script that needs to create shortcuts for all users must be run with administrative privileges. Right-click your .bat file and select "Run as administrator."
Problem: Handling Paths with Spaces
Forgetting to quote paths that contain spaces is a common source of errors.
Solution: Always Quote Your Paths
As shown in all the examples, every path variable (TARGET_EXE, SHORTCUT_NAME, etc.) should be enclosed in quotes both in the batch SET commands and inside the helper script strings to ensure they are handled correctly.
Practical Example: A Post-Installation Script
This script creates a shortcut for all users on the Public Desktop after a theoretical application installation. It uses the robust PowerShell method and includes custom properties.
@ECHO OFF
SETLOCAL
REM This script must be run as an Administrator.
SET "TARGET_EXE=C:\Program Files\MyCoolApp\MyCoolApp.exe"
SET "SHORTCUT_NAME=C:\Users\Public\Desktop\My Cool App.lnk"
SET "WORKING_DIR=C:\Program Files\MyCoolApp"
ECHO --- Post-Install: Creating Public Shortcut ---
ECHO.
IF NOT EXIST "%TARGET_EXE%" (
ECHO [ERROR] Target application not found. Aborting.
GOTO :End
)
ECHO Creating shortcut on the public desktop...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ws = New-Object -ComObject WScript.Shell; " ^
"$s = $ws.CreateShortcut('%SHORTCUT_NAME%'); " ^
"$s.TargetPath = '%TARGET_EXE%'; " ^
"$s.WorkingDirectory = '%WORKING_DIR%'; " ^
"$s.IconLocation = '%TARGET_EXE%,0'; " ^
"$s.Save()"
ECHO.
ECHO [SUCCESS] Shortcut created for all users.
:End
ENDLOCAL
-NoProfile and -ExecutionPolicy Bypass are good practices for PowerShell calls to ensure they run predictably.
Conclusion
Creating .lnk shortcuts from a batch script is a task that requires stepping outside of cmd.exe's native capabilities.
- The VBScript method is the classic solution, offering the best compatibility with older Windows systems at the cost of creating a temporary file.
- The PowerShell method is the modern and recommended best practice. It is cleaner, avoids temporary files, and is just as powerful.
For any deployment or setup script, using a PowerShell one-liner is the most professional and efficient way to manage shortcuts. Just remember to run your script as an administrator if you are creating them in shared or system locations.