Powershell determine new URL of a permanently moved (redirected) resource


I’m using Powershell Core v6-beta.5 using AppImage on Linux. Is there a way to find out the “new” location of a 301 redirect?

Invoke-WebRequest -Method HEAD http://SomethingThatThrows301.com/ -MaximumRedirection 0 throws an error (Response status code does not indicate success: 301 (Moved Permanently)).

While the error does mention that the move is a 301, I’d still like a proper object telling me that, and the new address.

Is there a way to do so?


Note: All code below works in both Windows PowerShell and PowerShell Core, on all supported platforms, with up to 50 redirections by default.


  • that you don’t care about the specific 3xx redirection status code and
  • that you only need to know the ultimate target URL (there could be a chain of redirections)

use the following:

This yields (note how the target URL has www.):

Below is the source code for advanced convenience function Get-UrlRedirection, which packages the functionality, offering both resolution to the ultimate target URL and an enumeration of the chain of redirection URLs.

Example calls:

In order to focus on the code, I’ve omitted the comment-based help above; here it is – simply paste it directly above the function definition:


Powershell determine new URL of a permanently moved (redirected) resource by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply