2

I am trying to download a file from a website with powershell and save it in a C:\temp dir with it's original name

if i try Invoke-WebRequest -Uri ("uri to file download") -Method Get with the "-OutFile" Parameter i can save the file but not with the original name. if i save the WebRequest return value in a parameter i find the original filename under $r.Headers.'Content-Disposition' but i don't know how to output "the file" out of this variable.

can any one help me ?

Kind regards Florian

ExeqZ
  • 23
  • 1
  • 3
  • 1
    Have you an Uri to share ? I don't remember to see a `Content-Disposition` in WebRequest headers – CFou Nov 20 '20 at 14:45
  • @CFou sadly i can not provide any url, because it is an internal domain. and i also didn't believed but i really nead to get the "regular" filename ^^ – ExeqZ Nov 23 '20 at 07:15

1 Answers1

0

I just created a quick snippet that should help you. Sadly, I couldn't test it without a valid URL :(

# Vars
$url = ""
$outputDir = $PSScriptRoot

# Invoke request
$result = Invoke-WebRequest -Method GET -Uri $url

# Extract name
$contentDisposition = $result.Headers.'Content-Disposition'
$fileName = $contentDisposition.Split("=")[1].Replace("`"","")
$path = Join-Path $outputDir $fileName

# Write into file
$file = [System.IO.FileStream]::new($path, [System.IO.FileMode]::Create)
$file.write($result.Content, 0, $result.RawContentLength)
$file.close()
vitalragaz
  • 308
  • 1
  • 10