Download files with Powershell and Invoke-WebRequest

While there are quite a few ways to download files with powershell, my usual go to if I’m limited to strictly powershell is Invoke-WebRequest.

Here’s an example:

1
Invoke-WebRequest https://urltofile.com/file -OutFile "C:\pathtofile.bleh"

While not as elegant as say wget or curl, it does the trick when there are no other options besides powershell.

Speaking of wget and curl, here are links for both. They work extremely well with powershell and give you a ton more options.

https://eternallybored.org/misc/wget/

https://curl.haxx.se/windows/

Added fun bonus. Here’s a little function that gives downloading via Invoke-WebRequest a little boost:

1
2
3
4
5
6
7
8
9
10
11
12
function Download-File {
    Param ($url)
    $get_file_name = $url.Split("/")[-1]
    $ProgressPreference = 'SilentlyContinue'
    $file_size = (Invoke-WebRequest $url -Method Head).Headers.'Content-Length' 
    $file_size_mb = $file_size/1MB
    Write-Host  - $get_file_name
    Write-Host  - $file_size_mb Megabytes
    write-Host  - Downloading file........
    Invoke-WebRequest $url -OutFile $env:USERPROFILE\Downloads\$get_file_name
    write-Host  - Download Finished!
}

Leave a Reply

Your email address will not be published. Required fields are marked *