In one of my recent Prof. PowerShell columns, I wrote about using the Wscript.Shell VBScript object in PowerShell to retrieve special folder paths. Another handy trick is the ability to create shortcut links to either file or web resources. Let me show you how to accomplish this in PowerShell and why you might want to.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The PowerShell version of this task is not much different from VBScript. First we create a reference to the Wscript.Shell object.
[cc lang="Powershell"]
PS C:\> $wshell=New-Object -ComObject "Wscript.Shell"
[/cc]
Then we call the CreateShortcut() method. We have to specify the full filename and path for the shortcut. We can create a web link using a .url extension or a file system shortcut using .lnk.
[cc lang="PowerShell"]
PS C:\> $link=$wshell.CreateShortcut("H:\Links\The Lonely Administrator.url")
[/cc]
The link now requires a target path.
[cc lang="PowerShell"]
PS C:\> $link.TargetPath="https://jdhitsolutions.com/blog"
[/cc]
Finally, the link won't be visible until we save it.
[cc lang="PowerShell"]
PS C:\> $link.Save()
PS C:\> dir h:\links
Directory: H:\links
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2/11/2011 8:56 AM 119 The Lonely Administrator.url
[/cc]
If you create a .lnk object then you can set a number of other properties like a Window style, icon and hot key. But I'll let you explore those on your own. But really, if this is all I'm doing PowerShell doesn't bring anything extra to the party. I might as well stick with VBScript.
So let's find something extra where PowerShell makes sense. I'll take a CSV file with new shortcut information, import it and create the relevant shortcuts. This is much easier to do in PowerShell than VBScript. First, here's my CSV file, or at least what it looks like when imported.
[cc lang="PowerShell"]
PS C:\> import-csv C:\work\Links.csv | format-table -AutoSize
Name Path TargetPath Type
---- ---- ---------- ----
The Lonely Administrator Desktop https://jdhitsolutions.com/blog url
ISO Files Desktop \\jdh-nvnas\files\iso lnk
Technet Script Center h:\links http://technet.microsoft.com/en-us/scriptcenter/default.aspx url
Scripting Guy Desktop http://blogs.technet.com/b/heyscriptingguy/ url
ScriptingAnswers h:\links http://www.scriptinganswers.com url
Tools h:\links \\jdh-nvnas\files\Tools lnk
Prof. PowerShell h:\links http://mcpmag.com/Articles/List/Prof-Powershell.aspx url
[/cc]
I have a mix of file and web links. The path indicates where to create the link. "Desktop" means use the Desktop special folder. Here's how easy this is:
[cc lang="PowerShell"]
$file="c:\work\links.csv"
$wshell=New-Object -ComObject "Wscript.Shell"
Import-CSV $file | foreach {
if ($_.Path -eq "Desktop")
{
$linkpath=Join-path -Path ($wshell.SpecialFolders.Item("Desktop")) `
-ChildPath "$($_.Name).$($_.type)"
}
else
{
$linkpath=Join-Path -Path $_.Path -ChildPath "$($_.Name).$($_.type)"
}
Write-Host "Creating $linkpath" -ForegroundColor Green
$link=$wshell.CreateShortcut($linkpath)
$link.TargetPath=$_.TargetPath
$link.Save()
Get-Item $linkpath
}
[/cc]
The only tricky part is using Join-Path to construct a filename and path. Notice that if the Path property on my imported object equals Desktop, I use the SpecialFolders object to retrieve that path. I could have used other special folders as well. I use Get-Item to write the file object to the pipeline in case I want to do anything else with it.
I hope you had some fun with this. But the real lesson I hope you'll take from it is that even though you can use VBScript objects in a PowerShell script, if you aren't taking advantage of PowerShell features and strengths, why bother?
If you would like to try my code and CSV file, you can download it here.
That’s slick 🙂
On a related note, check out http://poshcode.org/2493 … it has (really basic) support for all the extra stuff in shortcuts like “Run as Administrator” and setting the actual RGB values for console colors (so you can save and re-set your favorite sets of PowerShell colors), etc… but it’s really begging for someone to actually wrap that functionality into functions 😉