If you’re like me, you’ve got a browser favorites folder that is full of wonderful PowerShell code snippets. Ideally, it would be great to have a way to bring those favorite snippets into PowerShell so that my favorites folder becomes a code library.
Doing this presents several challenges. The first (and easiest to solve) is getting the web page the shortcut points to out of the .url file.
.url files are in the same sort of format as .ini files (each line has a property name, and =, and the value). So to get the url out of a shortcut file, I simply had to Get-Content on the file, pipe it to Where-Object, look for the start marker (url=), and get everything after that marker.
I decided to write this as an advanced function in CTP3, because I can make the parameters work well both with tab completed local files (e.g. .\MyShortcut.url) and with files passed in from Get-ChildItem. Making this happen is incredibly straightforward because of how ValueFromPipelineByPropertyName works with aliases. In the output of Get-ChildItem, the Fullname property will have full file name for me to use. Since I believe fullname is less intuitive than filename (and I’m trying to show some cool CTP3 scripting tricks), I’ve named my parameter and given it an alias of FullName. This means that items that are piped in with a property named either filename or fullname will populate $filename with the full path to the file.
Here’s a parameter signature that will accept both Resolve-Shortcut .\MyShortcut.url and Get-ChildItem -recurse | Resolve-Shortcut:
[Parameter( ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position = 0)]
[Alias("FullName")]
[string]
$fileName
And here is Resolve-ShortcutFile:
Synopsis:
Resolves an Internet shortcut file to the web site the shortcut references
Syntax:
Resolve-ShortcutFile [[-fileName] [<String>]] [-Verbose] [-Debug] [-ErrorAction [<ActionPreference>]] [-WarningAction [<ActionPreference>]] [-ErrorVariable [<String>]] [-WarningVariable [<String>]] [-OutVariable [<String>]] [-OutBuffer [<Int32>]] [<CommonParameters>]
Detailed Description:
Parses an Internet shortcut file and returns a property bag containing the
shortcut file and the URL it resolves to.
Examples:
-------------------------- EXAMPLE 1 -------------------------- # Resolves every shortcut in your favorites directory Get-ChildItem (Join-Path $env:UserProfile Favorites) -Recurse | Resolve-ShortcutFile
Command Parameters:
Name Description fileName The shortcut file name. Aliased to FullName so that it works well with the output of Get-ChildItem.
Here’s Resolve-ShortcutFile:
function Resolve-ShortcutFile { <# .Synopsis Resolves an Internet shortcut file to the web site the shortcut references .Description Parses an Internet shortcut file and returns a property bag containing the shortcut file and the URL it resolves to. .Parameter fileName The shortcut file name. Aliased to FullName so that it works well with the output of Get-ChildItem. .Example # Resolves every shortcut in your favorites directory Get-ChildItem (Join-Path $env:UserProfile Favorites) -Recurse | Resolve-ShortcutFile .Link Get-Content .Link Get-Item .Link Select-Object .Link Where-Object #> param( [Parameter( ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position = 0)] [Alias("FullName")] [string] $fileName ) process { if ($fileName -like "*.url") { Get-Content $fileName | Where-Object { $_ -like "url=*" } | Select-Object @{ Name='ShortcutFile' Expression = {Get-Item $fileName} }, @{ Name='Url' Expression = {$_.Substring($_.IndexOf("=") + 1 )} } } } }
Automatically generated with Write-CommandBlogPost
0 comments