6

#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!

Filed in: Columns, Tips and Tricks Tags: , ,
© 6480 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: