How-to: Create a Shortcut with Windows PowerShell

Create a shortcut in PowerShell:

$strTargetPath = "C:\demo\MyApp.exe"
$strLinkFile = "$env:Public\Desktop\MyShortcut.lnk"

$shell = New-Object -ComObject WScript.Shell
$Shortcut = $shell.CreateShortcut($strLinkFile)
$Shortcut.TargetPath = $strTargetPath
# $Shortcut.HotKey = "ALT+CTRL+F"
# $lnk.Arguments = "-accepteula"
# Set the window style (3=Maximized 7=Minimized 4=Normal)
$shortcut.WindowStyle = 4
$Shortcut.IconLocation = "%SystemRoot%\SystemResources\shell32.dll.mun, 94"
$Shortcut.Save()

# release the Wscript com object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell)
# In theory the global Garbage collector will automatically clean up old references
# but when calling com objects from .net this is not always reliable

Add a shortcut location to the Start Menu.

$StartMenu = [environment]::getfolderpath('StartMenu')
$shortcutPath = "$StartMenu\Programs\SS64"
If (!(test-path $shortcutPath)) {
  New-Item -ItemType Directory -Force -Path $shortcutPath
}
$strLinkFile = "$shortcutPath\demo shortcut.lnk"

Windows 'COM' has not been deprecated, but many com objects have been, see WScript.Shell on Microsoft.learn.

HotKey mappings are only usable if the shortcut is on the Desktop or in the Start Menu.

Valid hot key-options:

"ALT+", "CTRL+", "SHIFT+", and "EXT+".
"A" .. "Z", "0" .. "9", "Back", "Tab", "Clear", "Return", "Escape", "Space", and "Prior".

Shortcut Icons

Any DLL or other icon resource iconfile can be used as the source for a custom shortcut icon.
If you will be transferring the Shortcut file to another machine, the iconfile must be in a location that is available to that machine.
The index selects a specific icon within the file.

The following classic Icon Resource files are available on all Windows 10 and 11 machines:
%SystemRoot%\SystemResources\shell32.dll.mun
%SystemRoot%\SystemResources\imageres.dll.mun

Sample shell32.dll index numbers:
shell32.dll icon samples
Sample imageres.dll index numbers:
imageres.dll icon samples

In Desktop.ini files, a negative number for IconIndex indicates a resource ID (WinAPI) not a file reference.

Bonus: a small zip file with 19 simple color square Start-Menu .ico icons (Free/Public Domain).

Shortcut Auto LinkResolve

By default shortcuts will include the machine name of the destination, even for a local file like C:\MyFile.doc
This is not immediately visible until the shortcut file is copied to another machine, the shortcut target will then be automatically updated to point back to \\Machine1\c$\MyFile.doc
To turn this behaviour off add a DWORD value of 1 to the registry (before creating the shortcut):

HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
"LinkResolveIgnoreLinkInfo"=1

Shortcut NTFS file system tracking

If a shortcut to a file breaks because the destination file has moved, then by default Windows will attempt to automatically locate the shortcut destination by performing a search or matching file properties. This is done by the background service "TrkWks".

This can be turned on or off via the Group Policy Do not use the tracking-based method when resolving shell shortcuts or directly in the registry:

HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
NoResolveTrack

0 = disabled, 1 = enabled (REG_DWORD)

Advertised Shortcuts

An application shortcut created by .Net setup may not have the 'target' property enabled/visible. These shortcuts are known as 'Advertised Shortcuts' will perform Install-On-Demand, they will trigger installation of the application if it is missing (e.g. if the user has roamed to a different machine). After installation, the shortcut target can be decoded.

To disable the creation of an advertised shortcut during an msi install, use:
MSIEXEC.exe path_to_msi-file DISABLEADVTSHORTCUTS=1
or
setup.exe DISABLEADVTSHORTCUTS=1

Internet Shortcuts

Unlike file/folder shortcuts, Favourite (.URL) files are simple text files which you can create with a text editor or a couple of ECHO statements, they will open in the default browser:

Echo [InternetShortcut] > demo.url
Echo URL=https://ss64.com/ >> demo.url

Internet Explorer Pinned .website links

A file saved with a .website extension is an IE Pinned link. These are deprecated and by default will no longer open in IE11 or Microsoft Edge. The internal format is vaguely similar to the older .url file format, so simply renaming the file from .website to .url may mean that it opens in Edge. This is very much a dirty hack, so don’t bulk rename hundreds of .website files and expect them all to work.

Microsoft Edge Shortcuts

If MS Edge is not your default browser, but you want to open a web page using Edge, set an explorer Shortcut Target to a path like the following:

%windir%\explorer.exe microsoft-edge:https://ss64.com

Or if Edge is installed on the C: drive, you can simply do:

"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" https://ss64.com
Note that Edge installs itself in the x86 folder even though it is a 64 bit application.

Chrome Shortcuts

If Google Chrome is not your default browser, but you want to open a web page using Chrome, set the Shortcut Target to a path like the following, note that Chrome installs itself in the x86 folder even though it is a 64 bit application:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://ss64.com

Examples

Assuming the script above is saved in the current directory as new-shortcut.ps1 :

PS C:\> ./new-shortcut

“A great ad campaign will make a bad product fail faster. It will get more people to know it's bad” ~ William Bernbach.

Related PowerShell Cmdlets

New-Item -ItemType SymbolicLink
CMD: Shortcut.exe


 
Copyright © 1999-2024 SS64.com
Some rights reserved