Shortcut Properties

How do you create a shortcut in Windows via scripts? Easy use PowerShell! Below is the script to create a shortcut named “MyShortcut”. One can additionally create shortcuts to URLs too. I commented everything so it is easy to follow. The only parameter you must set is the $Shortcut.TargetPath everything else is optional.

The Windows Script Host (WSH) is a Windows administration tool. Within the Windows Script Host object model the root is Wscript and a sub-node which is WshShell (Wscript.Shell). WshShell object can run programs locally, modify the registry, create shortcuts, or access system folder. Specifically we’re interesting in creating shortcuts. We will access the WSH via the PowerShell script by utilizing the Windows Script Host (WScript) object (via COM) to create the shortcut.

Pro-tip: If you want to edit this powershell script or write your own checkout PowerShell_ISE.exe. It should be in your path already or it can be found in %windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe. If you want to know what a command does run, for example, Get-Help 'New-Object' -ShowWindow, will show the help in a window for New-Object. Show-Command can also be used to show all the commands the PowerShell supports.

Explaination

Required

  • Create a new Windows Script Host (“Wscript.Shell”) COM object. This is what we will use to create the shortcut with. $WShell = New-Object -ComObject "Wscript.Shell"
  • If we want a URL shortcut to the web, we can use the ‘url’ extension. Note, even though either extension can be used to represent local files, it is better to use lnk for local files and url for web URLs. $shortcutExtension = "lnk". The URL will output a plaintext .url file.
  • Call CreateShortcut with the path to where the shortcut will be written and extension (’lnk’).
  • Set target path for the shortcut. This is what the shortcut will point to when opened. $Shortcut.TargetPath
  • Finally, write/output the shortcut with $Shortcut.Save()

Optional

Before calling $Shortcut.Save() we can set the following:

$Shortcut member variable Description
Arguments Optional: arguments to pass, if executable
Description Optional: Comment to describe shortcut
Hotkey Optional: Hotkey to open the shortcut
IconLocation Optional: Icon to use (e.g., "%SystemRoot%\system32\imageres.dll,4"). Leave blank for Windows to pick the best icon
RelativePath Optional: Relative path
TargetPath Required: Path to directory or file to open
WindowStyle Optional: “Normal Window”= 1 (default), “Minimized”, “Maximized”
WorkingDirectory Optional: Working directory, or “Start in” in the shortcut

Result

A new shortcut called MyShortcut.lnk will be placed on the desktop. The target of the shortcut will be C:\.

Script

Save the following into a PowerShell script file (ps1).

###############################################################################
$WShell = New-Object -ComObject Wscript.Shell

###############################################################################
# Shortcut name
$shortcutName = "MyShortcut"

###############################################################################
# Shortcut extension
# for local files and directories
$shortcutExtension = "lnk"

# for URLs
#$shortcutExtension = "url"

###############################################################################
# Create the shortcut, the argument is the "FullName"
$Shortcut = $WShell.CreateShortcut( $WShell.SpecialFolders("Desktop") + "\" + $shortcutName + "." + $shortcutExtension)

###############################################################################
# Target
#$Shortcut.TargetPath = "%windir%\notepad.exe"
#$Shortcut.TargetPath = "\\localhost\"
$Shortcut.TargetPath = "C:\"

# Also can target URLs
#$Shortcut.TargetPath = "https://www.nullptr.org"

###############################################################################
# Arguments (Optional)
#$Shortcut.Arguments = "-myarg"

###############################################################################
# Start in (Optional)
#$Shortcut.WorkingDirectory = "WorkingDirectory"

###############################################################################
# "Shortcut key" (Optional)
#$Shortcut.Hotkey = "ALT+CTRL+F"

###############################################################################
# Window Style (AKA Run) (Optional)
# "Normal Window"= 1 (default), "Minimized", "Maximized"
#$Shortcut.WindowStyle = 1

###############################################################################
# Comment (Optional)
#$Shortcut.Description = "Comment"

###############################################################################
# Icon: only set for LNK shortcuts (Optional)

# Shell32.dll has the classic icons
#$Shortcut.IconLocation = "%SystemRoot%\System32\SHELL32.dll,4"

# Icon - imageres.dll the new icons ones
#$Shortcut.IconLocation = "%SystemRoot%\system32\imageres.dll,4"

###############################################################################
# Relative Path (Optional)
#$Shortcut.RelativePath = "myRelativePath"

###############################################################################
# Write the shortcut
$Shortcut.Save()

###############################################################################
# Cleanup (Include if part of a larger script)
Remove-Variable Shortcut
Remove-Variable WShell