Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun – Snappy Shortcuts

Posted on February 11, 2011March 28, 2011

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Friday Fun – Snappy Shortcuts”

  1. Pingback: Tweets that mention Friday Fun – Snappy Shortcuts | The Lonely Administrator -- Topsy.com
  2. Joel "Jaykul" Bennett says:
    February 12, 2011 at 1:23 am

    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 😉

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d