URL Shortening

Love it or loathe it, URL shortening has been with us a while now and can certainly be handy. TinyURL are one such company to offer this service. Nicely for us, we do not need to register in order to use their API, and yet nicer still is that we can use it simply by entering a standard format of URL.

Before we see how we can use PowerShell to automate this process, let’s take a look at the format of URL that we need to use with TinyURL.


Where targetaddress refers to the URL that you wish to shorten.

And that’s it.

Let’s say we wanted share a link containing information about this years PowerShell Summit Europe event in Stockholm. The full length URL for this is :


If we wanted to get the TinyURL equivalent of this, we’d use the following URL, pasting it into the address bar of our browser.



For making this happen via PowerShell, Invoke-WebRequest is our friend. All we need to do is provide the required address via the Uri parameter, and the Content property of the returned HtmlWebResponseObject will contain its shortened equivalent.

So for the case of the above we’d be using a command (note the pipeline symbol) of the type :

Invoke-WebRequest -Uri 'http://tinyurl.com/api-create.php?url=http://powershell.org/wp/community-events/summit/powershell-summit-europe-2015/' |
Select-Object -ExpandProperty Content

And can expect to get :


I’ve put together a cmdlet called Get-TinyURL for doing this. At its simplest, you can run it with the Uri parameter, and it will return a PSObject containing the original full address and its shortened equivalent.

Get-TinyURL -Uri 'http://powershell.org/wp/community-events/summit/powershell-summit-europe-2015/'



It’s also been bulked out a bit to give some extra functionality, such as being able to read from and write to the clipboard if we want. With both options enabled, we can copy a full address into the clipboard, run the cmdlet, and automatically have the shortened URL available for pasting wherever we want it next.

Navigate to desired URL and copy it to the clipboard

Get-TinyURL -ReadClipboard -WriteClipboard

Run the required command

pseuemail Paste where required

The code used is listed below, and will also be posted on GitHub in due course.

function Get-TinyURL
        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True,ParameterSetName = 'URI')] [string] $Uri,
        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True,ParameterSetName = 'ReadClipboard')] [switch] $ReadClipboard,
        [Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)] [switch] $WriteClipboard = $False
        If ($ReadClipboard -or $WriteClipboard) 
            $null = Add-Type -AssemblyName System.Windows.Forms
        If ($ReadClipboard) 
            $Uri = [system.windows.forms.clipboard]::GetData('System.String')
        $tinyURL = Invoke-WebRequest -Uri "http://tinyurl.com/api-create.php?url=$Uri" | 
        Select-Object -ExpandProperty Content
        If ($WriteClipboard) 
        $hash = @{
            Uri     = $Uri
            TinyURL = $tinyURL
        New-Object -TypeName PsObject -Property $hash

4 thoughts on “URL Shortening

    • Thanks Jaap. WordPress seems to do this automatically, and still trying to work out how to prevent it formatting things this way. Got any pointers on this? I’m new to WordPress.

      • I installed a plugin WP-Syntax, which handles the code highlighting for me. It isn’t actively being updated anymore so there might be better plugins for it, but it highlights my PowerShell code. I never had any issues with additional characters appearing so it’s good in that sense, here is the plugin page:

  1. Well, ironies of ironies. I ended up writing a PowerShell cmdlet to convert the code into WordPress compatible HTML format. Think that will be a future blog post. lol.

Leave a Reply

Your email address will not be published. Required fields are marked *