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

Making Short Links Long with PowerShell and WPF

Posted on July 9, 2018

Sometimes, when I have nothing better to do, I kill some time giving Todd Klindt and Shane Young a hard time during their podcast. You should join me sometime. Anyway, during a recent show Todd mentioned a bit of PowerShell code he put together to resolve short links. You see these all the time in your Twitter stream and others. What is that link really pointing to? Todd's solution was to use Invoke-Webrequest  to access the link and then look at the uri from the response. I took his code, as I usually do, and ran with it - straight into a simple WPF form.

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!

Here's the essence of Todd's original code.

$short = https://bit.ly/PSMoL3
$wr = Invoke-WebRequest -UseBasicParsing –Uri $short
$wr.baseresponse.ResponseUri.AbsoluteURI

If you run this you should get the result https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches-third-edition. I figured is might be nice to have a form where I could copy and paste a link from Twitter and get the resolved link. So I put together a WPF-based script that uses a stack panel to display a simple input box.

 

image

Clicking OK runs the code in the associated scriptblock to resolve the link using Invoke-Webrequest and then closes the form. The function writes the result, which is an object, back to the pipeline.

image

Depending on the resolved link you may not see all of it, but hopefully enough to know whether you trust it. Or run the Resolve-ShortLink function and pipe it to Format-List.

You can get the code for my function from GitHub.

If you have problems or suggestions please post an issue on the gist page.

I hope you'll give it a try. Now if you'll excuse me I have some online heckling to attend to.


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 “Making Short Links Long with PowerShell and WPF”

  1. postanote says:
    July 9, 2018 at 3:21 pm

    I like reviewing how other folks approach thing, and the in cool and all but, why the use of a form for this, just to be thrown back to the console, when you can just use a function with a param and stay in the console (still using copy and paste)?

    Using this function…

    Function Resolve-TinyUrl
    {
    [CmdletBinding()]

    [Alias(‘rturl’)]

    Param(
    [Parameter(Mandatory=$true,
    HelpMessage=”Enter a valid Url”)]
    [ValidatePattern(‘(?# Enter a valid Url)^(ftp:|http:|https:|ftp:)(//.*)’)]
    [string]$ShortUrl
    )

    “The URL entered – $ShortUrl is valid”

    Start-Sleep -Seconds 1

    $longURL = (Invoke-WebRequest -UseBasicParsing –Uri $ShortUrl).baseresponse.ResponseUri.AbsoluteURI
    “The ShortUrl ‘$shortUrl’ Resolves to -> ‘$longUrl'”
    }

    … or for a bit more info or control aspects, this one

    Function Expand-Url
    {
    [CmdletBinding()]

    [Alias(‘eurl’)]

    Param(
    [Parameter(Mandatory=$true,
    HelpMessage=”Enter a valid Url”)]
    [ValidatePattern(‘(?# Enter a valid Url)^(ftp:|http:|https:|ftp:)(//.*)’)]
    [string]$URLRaw
    )

    “The URL entered – $URLRaw is valid”

    Start-Sleep -Seconds 1

    (Invoke-WebRequest -Uri $URLRaw -MaximumRedirection 0 -ErrorAction Ignore).Headers.Location
    $result = [System.uri](Invoke-WebRequest -Uri $URLRaw -MaximumRedirection 0 -ErrorAction Ignore).Headers.Location

    “Port: $($result.port)”
    “Page Authority: $($result.Authority)”
    “Served using: $($result.Scheme)”
    }

    1. Jeffery Hicks says:
      July 9, 2018 at 3:28 pm

      I agree it is just as easy to do this from the console. That’s what Todd Klindt was originally doing. I simply felt challenged to see if I could whip up a WPF script in the time it took him to talk about his code. There’s no added benefit of doing it with a form. And actually, if I were to continue with this I’d display result directly in the form. Maybe even write the code to use a runspace so it can run continuously. Lots of possibilities.

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