#PSTip Retrieve a redirected URL – PowerShell 3.0 way!

Note: This tip requires PowerShell 3.0 or above.

In an earlier tip, we looked at how we can retrieve the redirected URL using a .NET class. In today’s tip, we will look at how we can simplify that process using an in-built PowerShell 3.0 cmdlet – Invoke-WebRequest.

$uri = 'http://go.microsoft.com/fwlink/?LinkID=210601'
$request = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -ErrorAction Ignore

if($request.StatusDescription -eq 'found')
{
   $request.Headers.Location
}

This is it. Much simpler than the earlier approach!

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: