Technology Consulting

Creating a Windows Shortcut with PowerShell

Learn how to automate the process of creating a Window Shortcut (.lnk file) with PowerShell

Windows Shortcuts are a useful way to have quick access to your apps from your Desktop or Start Menu.

The following script shows you how to create a Shortcut using PowerShell.

## %%%% Setup Goes Here %%%%%

# Where will the Shortcut be saved?
$ShortcutPath = "$($Home)\Desktop\My App Shortcut.lnk"

# Where should the Shortcut point to?
$TargetPath = "$($Env:SystemDrive)\Program Files\My App\My App.exe"


## %%%% Stuff Actually Happens Here %%%%%

# Create a Windows Script Host shell instance
$Scripter = New-Object -COMObject WScript.Shell

# Create a shortcut file that will be saved at the specified location
$Shortcut = $Scripter.CreateShortcut($ShortcutPath)

# Set the shortcut's Target Path
$Shortcut.TargetPath = $TargetPath

# Save the shortcut file
$Shortcut.Save()

We set the variable $ShortcutPath to the location where we want to save the Shortcut. In this case, it's the Desktop, and we want to name it My App Shortcut. $Home is a convenient, built-in PowerShell variable that we can use to get our Home directory, eg. c:\Users\coreyklass. The file path here then becomes c:\Users\coreyklass\Desktop\My App Shortcut.lnk. Windows Shortcuts always use the file extension .lnk.

$ShortcutPath = "$($Home)\Desktop\My App Shortcut.lnk"

Next we set the variable $TargetPath to the location where the Shortcut will point. Here we grab the environment variable %SystemDrive% using the PowerShell statement $Env:SystemDrive, eg. C:. The file path here then becomes C:\Program Files\My App\My App.exe.

$TargetPath = "$($Env:SystemDrive)\Program Files\My App\My App.exe"

Now that our file path variables are set, we can begin the Shortcut creation process. The simplest way of doing this is to create an instance of the Windows Script Host and assign it to the variable $Scripter.

$Scripter = New-Object -COMObject WScript.Shell

We tell the Windows Script Host instance to create a new Shortcut at our $ShortcutPath.

$Shortcut = $Scripter.CreateShortcut($ShortcutPath)

We assign the target path of the Shortcut to the $TargetPath.

$Shortcut.TargetPath = $TargetPath

Finally we save the Shortcut to the .lnk file.

$Shortcut.Save()

And there's now a pretty new Shortcut on your Desktop that will save you hours of time from lost productivity.