I will guide you through creating shortcuts for files, folders, or applications using a PowerShell script in this post. This method can be particularly useful for creating shortcuts across multiple machines or for all Active Directory users within an organization.
I recently encountered a need to create shortcuts for an application on the desktops of all Active Directory user machines. After exploring various methods, I found that a PowerShell script offered the most efficient and scalable solution.
To create a shortcut using PowerShell, readers will utilize the $WShell
object from the WScript.Shell
COM object. This object provides the necessary methods and properties for creating and manipulating shortcuts.
Below is a basic PowerShell script that demonstrates how to create a shortcut for a specified target (file, folder, or application):
$SourcePath = "C:\Path\To\TargetFile.exe" # Replace with the actual path of the target
$ShortcutPath = "C:\Users\Public\Desktop\ShortcutName.lnk" # Replace with the desired shortcut path and name
$WShell = New-Object -ComObject WScript.Shell
$Shortcut = $WShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = $SourcePath
$Shortcut.Save()
Let’s break down the script:
$SourcePath
: This variable holds the path to the target file, folder, or application for which you want to create a shortcut. Replace"C:\Path\To\TargetFile.exe"
with the actual path of your target.$ShortcutPath
: This variable specifies the path and name of the shortcut file you want to create. In this example, it creates a shortcut namedShortcutName.lnk
on the Public Desktop. Modify the path and name as per your requirements.$WShell = New-Object -ComObject WScript.Shell
: This line creates a new instance of theWScript.Shell
COM object, which provides methods and properties for creating and manipulating shortcuts.$Shortcut = $WShell.CreateShortcut($ShortcutPath)
: This line creates a new shortcut object at the specified$ShortcutPath
location.$Shortcut.TargetPath = $SourcePath
: This line sets the target path of the shortcut to the$SourcePath
variable, which points to the file, folder, or application you want to create a shortcut for.$Shortcut.Save()
: This line saves the shortcut object to the specified$ShortcutPath
location, creating the shortcut file.
After running this script, you should find a new shortcut created at the specified $ShortcutPath
location, which will open the target file, folder, or application when double-clicked.
You can easily modify this script to create shortcuts for multiple targets or distribute the shortcuts to specific user desktops or network locations using additional PowerShell commands and techniques.
PowerShell Create Shortcut to File
Let’s say you want to create a shortcut for Google Chrome using a PowerShell script. Here’s an example script that demonstrates how to create a shortcut for Google Chrome on the desktop:
# Path to the Google Chrome executable
$SourceFilePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
# Path and name for the new shortcut
$ShortcutPath = "$env:PUBLIC\Desktop\Google Chrome.lnk"
# Create a WScript.Shell object
$WShell = New-Object -ComObject WScript.Shell
# Create a new shortcut object
$Shortcut = $WShell.CreateShortcut($ShortcutPath)
# Set the target path for the shortcut
$Shortcut.TargetPath = $SourceFilePath
# Set the description for the shortcut
$Shortcut.Description = "Google Chrome Web Browser"
# Set the working directory for the shortcut
$Shortcut.WorkingDirectory = "C:\Program Files (x86)\Google\Chrome\Application"
# Set the icon for the shortcut
$Shortcut.IconLocation = "$SourceFilePath,0"
# Save the shortcut
$Shortcut.Save()
Here’s what the script does:
$SourceFilePath
specifies the full path to the Google Chrome executable (chrome.exe
). Adjust the path according to your installation location.$ShortcutPath
specifies the path and name for the new shortcut file. In this example, the shortcut will be created on the Public Desktop with the name “Google Chrome.lnk”.- The script creates a new instance of the
WScript.Shell
COM object, which provides methods and properties for creating and manipulating shortcuts. $Shortcut = $WShell.CreateShortcut($ShortcutPath)
creates a new shortcut object at the specified$ShortcutPath
location.$Shortcut.TargetPath
sets the target path of the shortcut to the$SourceFilePath
variable, which points to the Google Chrome executable.$Shortcut.Description
sets a description for the shortcut.$Shortcut.WorkingDirectory
sets the working directory for the shortcut, which is the directory that Google Chrome will start in when launched from the shortcut.$Shortcut.IconLocation
sets the icon for the shortcut using the path to the Google Chrome executable and the icon index (0 for the first icon in the executable).$Shortcut.Save()
saves the shortcut object to the specified$ShortcutPath
location, creating the shortcut file.
After running this script, you should find a new shortcut named “Google Chrome.lnk” on your Public Desktop. When you double-click this shortcut, it will launch Google Chrome.
Note: make sure you have the Windows Script Host (WSH
) installed on your computer to run this script. If you encounter any issues, ensure that you have the required permissions and the paths specified in the script are correct.
PowerShell Create a shortcut to Folder.
Now, I’ll explain how to create a shortcut to a folder on the user’s desktop or on a remote computer using PowerShell.
Let’s consider that we need to create a shortcut for a folder located at D:\MyFolder
and save the shortcut on the user’s desktop.
Here’s the PowerShell script to achieve this:
# Path to the folder for which you want to create a shortcut
$SourceFolderPath = "D:\MyFolder"
# Get the current user's desktop path
$DesktopPath = [Environment]::GetFolderPath("Desktop")
# Construct the shortcut path and name on the desktop
$ShortcutPath = Join-Path -Path $DesktopPath -ChildPath "MyFolder Shortcut.lnk"
# Create a WScript.Shell object
$WShell = New-Object -ComObject WScript.Shell
# Create a new shortcut object
$Shortcut = $WShell.CreateShortcut($ShortcutPath)
# Set the target path for the shortcut
$Shortcut.TargetPath = $SourceFolderPath
# Set the description for the shortcut
$Shortcut.Description = "Shortcut to MyFolder"
# Save the shortcut
$Shortcut.Save()
Here’s what the script does:
$SourceFolderPath
specifies the full path to the folder for which you want to create a shortcut.$DesktopPath
retrieves the current user’s desktop path using the[Environment]::GetFolderPath("Desktop")
method.$ShortcutPath
constructs the path and name for the new shortcut file on the desktop usingJoin-Path
cmdlet.- The script creates a new instance of the
WScript.Shell
COM object, which provides methods and properties for creating and manipulating shortcuts. $Shortcut = $WShell.CreateShortcut($ShortcutPath)
creates a new shortcut object at the specified$ShortcutPath
location.$Shortcut.TargetPath
sets the target path of the shortcut to the$SourceFolderPath
variable, which points to the folder you want to create a shortcut for.$Shortcut.Description
sets a description for the shortcut.$Shortcut.Save()
saves the shortcut object to the specified$ShortcutPath
location, creating the actual shortcut file.
After running this script, you should find a new shortcut named “MyFolder Shortcut.lnk” on your desktop. When you double-click this shortcut, it will open the D:\MyFolder
folder.
To create the shortcut on a remote computer, you can use PowerShell remoting or invoke the script on the remote machine using PsExec or similar tools. The script will create the shortcut on the desktop of the user logged in to the remote computer.
Make sure to replace D:\MyFolder
with the actual path to the folder for which you want to create a shortcut, and modify the shortcut name if desired.
Leave a Reply
View Comments