Using PowerShell to Create a Shortcut

PowerShell's New-Object -Com Creates a Shortcut

The purpose of this page is to explain how to create a shortcut on your desktop.  The method we will be using is, New-Object -ComObject WScript.Shell.

Topics for PowerShell Creates Desktop Shortcut

 ♣

PowerShell's New-Object -Com

All PowerShell COM objects are rely on the basic command: New-Object -Com.  For our mission to create a shortcut we need a WScript.Shell type of comObject.  As usual, I will take you step-by-step through the method.

1) Assuming You Have Installed PowerShell
Launch the ISE (GUI) or if you prefer, the PowerShell command line; then issue these commands:

2) Create the Object (WScript.Shell)
It is convenient to create an object and assign it to a variable, for example:

# Windows PowerShell Com Object
$WshShell = New-Object -ComObject WScript.Shell

3) Research the Methods with Get-Member
Creating the com object is like creating a shortcut shell, next we need to supply properties using the correct .method.

# Research WScript.Shell Methods
$WshShell = New-Object -ComObject WScript.Shell
$WshShell | Get-Member -memberType methods

Learning Points

The above is how I discovered the .CreateShortcut method.  Let us now see how we can build a shortcut on the desktop.

4) Build a Shortcut Shell

Problem: This example below merely creates a shell, we are going to need properties for a fully functioning shortcut see 5) Create Shortcut.

# Build a Shortcut Shell
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\MyFirstShortCut.lnk")
$Shortcut | Get-Member -memberType Properties

Learning Points

Note 1: $Home\Desktop specifies the location for the shortcut, this translates to the desktop of the current user.

Note 2: Here are the shortcut properties some of which are vital to build a functioning shortcut.

Arguments Property
Description Property
FullName Property
Hotkey Property
IconLocation Property
RelativePath Property
TargetPath Property
WindowStyle Property
WorkingDirectory Property

5) Create a Functioning Shortcut on Your Desktop

This example uses PowerShell to create a shortcut for calc.exe on your desktop.  You could substitute the name of another executable, or if you prefer type the full path to the program you want to launch via a desktop shortcut.

# Create a Calculator Shortcut with Windows PowerShell
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\Calc.lnk")
$Shortcut.TargetPath = "Calc"
$Shortcut.Save()

Note 3:  Nothing much happens unless you append the .Save() method.

Note 4:  It's essential to put the name of the application "Calc", or the path, in double speech marks.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Another PowerShell Shortcut Example

The purpose of this script is to create a different shortcut, one which allows you to remove USB hardware quickly, but safely.

PowerShell Script to Create a Shortcut

$AppLocation = "C:\Windows\System32\rundll32.exe"
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\USB Hardware.lnk")
$Shortcut.TargetPath = $AppLocation
$Shortcut.Arguments ="shell32.dll,Control_RunDLL hotplug.dll"
$Shortcut.IconLocation = "hotplug.dll,0"
$Shortcut.Description ="Device Removal"
$Shortcut.WorkingDirectory ="C:\Windows\System32"
$Shortcut.Save()

Note 5: This example employs more properties, for example, .Arguments and .IconLocation.

Troubleshooting PowerShell Create Shortcut

The secret of troubleshooting PowerShell in general, and shortcuts in particular, is to have a manual walk-through of the process.Create Shortcut to Hotplug.dll

  • Right-click on the desktop.
  • Select 'New'.
  • Select 'Shortcut'
  • Type Calc
  • Next
  • Finish

Another source of troubleshooting a shortcut that PowerShell did not make correctly is to compare the failure with the properties of a shortcut you successfully created manually.  My idea is to match up the properties as researched with Get-Member with boxes such as Target, or Start in.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

SolarWinds' Network Performance Monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds' Network Performance Monitor

How to List Shortcuts

Here is a script to list shortcuts on Microsoft's Start Menu

# List all .lnk files and their image paths
Clear-Host
$Path = "$Env:ProgramData\Microsoft\Windows\Start Menu\Programs"
$StartMenu = Get-ChildItem $Path -Recurse -Include *.lnk
ForEach ($Item in $StartMenu) {
   $Shell = New-Object -ComObject WScript.Shell
   $Properties = @{
        ShortcutName = $Item.Name
        Target = $Shell.CreateShortcut($Item).targetpath
            }
New-Object PSObject -Property $Properties
}
[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null

See more PowerShell COM Object tasks ยป

Summary of PowerShell Create Shortcut Script

I have employed the New-Object cmdlet to mimic what you do manually when creating a shortcut on the desktop.  Because it's trickier than you might suspect, I have built-up gradually, in particular, I explained how to research the methods and properties.

If you like this page then please share it with your friends

 


See More Windows PowerShell Examples of Real-life Tasks

PowerShell Tutorials  • PowerShell Examples  • Get-Counter  • IpConfig   • PowerShell v3 Ipconfig

Monitor Performance - PowerShell   • PowerShell Create Shortcut   • PowerShell Function Shortcut

PowerShell Restore Computer  • PowerShell Temp  • PowerShell Get-Item Env:  • PowerShell NetSh

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

 

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: