Aquire the url that is being redirected to when using the passed url parameter
    When a website only supplies a url to the latest version, but that url is redirected
    to a different url to aquire the actual binary file.
    Then this function can be used to simplify that aquiral.
    The url to check for redirection
.PARAMETER referer
    An optional parameter to use when a website requires the referer header to
    be used
    The redirected url when one is found, otherwise returns the same url that was passed.

function Get-RedirectedUrl {
        [Parameter(Mandatory = $true)]

    $req = [System.Net.WebRequest]::CreateDefault($url)
    if ($referer) {
        $req.Referer = $referer

    $resp = $req.GetResponse()

    if ($resp -and $resp.ResponseUri.OriginalString -ne $url) {
        Write-Verbose "Found redirected url '$($resp.ResponseUri)"
        if ($NoEscape -or $($resp.ResponseUri.OriginalString) -match '\%\d+' ) {
            $result = $resp.ResponseUri.OriginalString
        else {
            $result = [uri]::EscapeUriString($resp.ResponseUri.OriginalString)
    else {
        Write-Warning "No redirected url was found, returning given url."
        $result = $url


    return $result