Desktop Shortcut with WScript.Shell

Expanding on this topic discussion further,

PS C:\> $wshshell = New-Object -ComObject WScript.Shell
PS C:\> $lnk = $wshshell.CreateShortcut (”$home\Desktop\myhome.lnk”)     #shortcut must have a file extension of .lnk or .url

The $wshshell.CreateShortcut() method does not actually save the shortcut until the $lnk.Save() method is explicitly called. This means that the former can be used to obtain a reference to the shortcut if it already exists.

You use $lnk.TargetPath property to specify or modify the actual item itself, be it an executable, a document or folder. This maps to the Target field of the GUI (on the shortcut tab) when the properties of the link is inspected.

The $lnk.FullName property returns the fully qualified path of the shortcut such as C:\Documents and Settings\administrator\desktop\myhome.lnk (maps to General > Location on GUI).

Some other useful operations:

$lnk.Arguments = “filename.ext”      #appends the arguments to the $lnk.TargetPath property

$lnk.WorkingDirectory = “C:\temp”      #maps to Start in: on GUI

$lnk.Description = “This is my home folder”      #corresponds to the Comment field on the shortcut tab.

No in-built method exists to rename a Windows Desktop Shortcut. This is actually not necessary because a shortcut is stored just like your regular file or folder in the underlying file system, and the Rename-Item cmdlet can be deployed:

Rename-Item $lnk.FullName “myNewHome.lnk” -PassThru

Similarly, a shortcut can be deleted using the Remove-Item cmdlet (will not show up in the Recycle Bin):

Remove-Item “$home\Desktop\myNewHome.lnk”

1 Comment »

One Response to “Desktop Shortcut with WScript.Shell”

  1. Verknüpfungen anlegen mit Powershell « Uwes kleines Technikblog on 02 Oct 2009 at 9:13 pm #

    […] Im Blog von Lee Desmond findet sich ein kurzer Artikel, wie man über die Powershell Verknüpfungen anlegen kann: http://www.leedesmond.com/weblog/?p=60. […]

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.