Powershell : Download files with weird characters

In the world of weird requirements I had a requirement to download some software from a server, the download link contained a "rm" token which was a GUID wich can be seen below with the URL address:


When you visit this URL it would download a file like this, which is a problem as it has weird characters that scripting does not like contained within, these are shown in bold:


Therefore if you tried to download via Powershell you get the error linking to wildcard paths as below:

Invoke-WebRequest : Cannot perform operation because the wildcard path 

This means any character in the filename that is not a word character (alphanumeric or underscore), a dot (.), or a hyphen (-) with an underscore (_) this then ensures the script can handle the download.

$url = "https://magicalwebsite.a6n.co.uk/download_client_data?fn=null&rm=5cf708e4757ec6ab2ddc97c52eaa9a20&p=winNT-64&ss=511d97152d4610abafbeb2d4f6995af350071ddf%22"

$outputFolder = "C:\Software-Download"
$response = Invoke-WebRequest -Uri $url -Method Head
$fileName = $response.Headers['Content-Disposition'] -replace '.*filename=',''
$fileName = $fileName -replace '[^\w\.-]', '_' # Replace invalid characters with underscores

$outputFile = Join-Path -Path $outputFolder -ChildPath $fileName

Invoke-WebRequest -Uri $url -OutFile $outputFile

Previous Post Next Post

Ω†Ω…ΩˆΨ°Ψ¬ Ψ§Ω„Ψ§ΨͺΨ΅Ψ§Ω„