My Profile Photo

Matthew Hodgkins Blog

❤️er of Automation, AWS, DevOps, CI/CD, Python, Golang and Observability. Aussie living in the Netherlands.

Downloading a file with PowerShell without specifying its name

I’m going to show you how to download a file from the web using PowerShell without having to specify the filename on download.

This definately falls into the category of “why is this so hard in PowerShell?”. Looks like some other folks agree:

Usually you would need to use -Outfile to download a file, for example:

Invoke-WebRequest -Uri "" -Outfile "MicrosoftEdgeSetup.exe"

How does the browser know what file to give the download though?

Edge Download

Let’s find out:

$download = Invoke-WebRequest -Uri ""


With that we can see the name of the download:


To get the file name out:

$content = [System.Net.Mime.ContentDisposition]::new($download.Headers["Content-Disposition"])

Show Filename

Now we could take that file name and run another Invoke-WebRequest with a -Outfile parameter, but that would involve downloading the entire file again.

Let’s save the contents of our $download variable to disk.

$fileName = $content.FileName

$file = [System.IO.FileStream]::new($fileName, [System.IO.FileMode]::Create)
$file.Write($download.Content, 0, $download.RawContentLength)

Now we have MicrosoftEdgeSetup.exe saved.


Here is the Save-Download function which makes this process easier: