Skip to content

Instantly share code, notes, and snippets.

@tadcrazio
Last active June 29, 2017 15:06
Show Gist options
  • Save tadcrazio/647894e10e18984589d452d25317280c to your computer and use it in GitHub Desktop.
Save tadcrazio/647894e10e18984589d452d25317280c to your computer and use it in GitHub Desktop.
Powershell function to make a URL tiny.
Add-Type -AssemblyName System.Web
function Get-TinyURL
{
<#
.SYNOPSIS
Make a URL tiny.
.DESCRIPTION
Enter a URL to make tiny. Will encode URL's ideal for url's that have reserverd characters.
.PARAMETER URL
The url you wish to make tiny.
.EXAMPLE
Get-TinyUrl -URL www.google.com
.NOTES
Nothing to see here.
#>
[OutputType([string])]
param
(
[Parameter(Mandatory = $true,
Position = 0,
HelpMessage = 'Long URL')]
[ValidateNotNullOrEmpty()]
[string]$URL
)
try
{
if ($URL -notcontains '^https?://')
{
$URL = 'http://' + $URL
}
$Newurl = [System.Web.HttpUtility]::UrlEncode($URL)
Invoke-RestMethod -Uri "http://tinyurl.com/api-create.php?url=$Newurl"
}
catch
{
Write-Output "Doh! That didn't work.`n $_.Exception.Message"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment