Use a PowerShell function to get location of a short URL

by Bharat Suneja

Quick function to get a location (a target or long URL) for a short URL.

URL shortening services return the target or long URL for a short URL using the location property of HTTP status code 301 (Moved Permanently).

Add this function to your PowerShell profile to get the location for any short URL.

#Function to expand shortURL and get longURL
Function Get-LongURL {
 param (
       [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
       $shortURL 
        )  
  $header= invoke-webrequest -uri $shortURL -MaximumRedirection 0 -ErrorAction SilentlyContinue
  if ($header.StatusCode -eq 301 -or $header.StatusCode -eq 302){
     Write-host "Status Code: " $header.StatusCode $header.StatusDescription "`nLocation:"$header.Headers.Location
  }
     else {
          Write-Host "Not redirected, status code:" $header.StatusCode $header.StatusDescription
          }
}

{ 0 comments… add one now }

Leave a Comment

Previous post: