Resolving URLs

by Jun 10, 2019

Often, URLs redirect to the final URL, so if you’d like to know where a given URL really points to, use a function like this:

function Resolve-Url
  $request = [System.Net.WebRequest]::Create($url)
  $response = $request.GetResponse()
  $url = $response.GetResponseHeader("Location")
  return $url

For example, the latest PowerShell release is always available via this url:

By resolving this URL, you get back the currently active latest URL which in turn is a quick way of finding the latest PowerShell version available:

PS C:\> Resolve-Url -url

PS C:\> ((Resolve-Url -url -split '/')[-1]

PS C:\> [version](((Resolve-Url -url -split '/')[-1] -replace 'v')

Major  Minor  Build  Revision
-----  -----  -----  --------
6      2      1      -1  

Twitter This Tip! ReTweet this Tip!