Search code examples
powershelluripowershell-5.1

PowerShell [uri] Types Accelerator produces different AbsoluteUri


I have a local file ($URLsFile), which contains a list of URLs:

http://WIND451325/Dev/Application.aspx__%__dev.com
http://WIND451326/Dev/Application.aspx__%__dev.com

When using the [uri] Types Accelerator with the Get-Content command:

[uri[]]$ArrayURLs = Get-Content -Path $URLsFile

I've noticed some changes in the value of the AbsoluteUri property, as __%__ changed to __%25__ :

foreach ($item in $ArrayURLs)
{
    $item.AbsoluteUri
}

http://WIND451325/Dev/Application.aspx__%25__dev.com
http://WIND451326/Dev/Application.aspx__%25__dev.com

After going through the official documentation:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_type_accelerators?view=powershell-5.1

https://learn.microsoft.com/en-us/dotnet/api/system.uri?view=net-7.0

I could not find any reference to the subject.


Solution

  • You can use the overload with dontEscape set to $true.

    $ArrayURLs = Get-Content -Path $URLsFile |
        ForEach-Object { [uri]::new($_, $true) }
    

    This ctor is obsolete, in newer versions of PowerShell 7+, you should use the UriCreationOptions overload instead.

    $options = [System.UriCreationOptions]@{
        DangerousDisablePathAndQueryCanonicalization = $true
    }
    
    $ArrayURLs = Get-Content -Path $URLsFile |
        ForEach-Object { [uri]::new($_, [ref] $options) }