< Back to Notes

curl to Invoke-WebRequest (PowerShell)

Sometimes you might come across a download the offers a handy curl link for downloading in a terminal. But you need the download on a Windows machine. That doesn't have curl installed. And you cannot install it. And you need to download in CLI mode.

There is a curl command in PowerShell, but it is likely the default alias to PowerShell's Invoke-WebRequest, not the actual curl tool. Run alias curl in PowerShell to check if it's an alias. Pasing curl's arguments verbatim to Invoke-WebRequest of course won't work, the syntax is very different.

Invoke-WebRequest requires PowerShell 3 or later. Run (Get-Host).Version to check the PowerShell version.

Let us analyze a basic example on the following made-up curl command – it would download a file that requires authentication which is done with a bearer token:

curl -O -H "Bearer: AbCdEfGH-0123456789;" "https://example.com/downloads/app-x64-20651.zip"

Let us analyze the curl parameters first:

The equivalent Invoke-WebRequest parameters are (replace underlined parts with actual values):

The equivalent of the curl example with Invoke-WebRequest would be:

Invoke-WebRequest `
-OutFile 'app-x64-20651.zip' `
-Headers @{'Bearer' = 'AbCdEfGH-0123456789';} `
-Uri 'https://example.com/downloads/app-x64-20651.zip'

Invoke-WebRequest reference

Tip on the side: Line continuation character in PowerShell is the backtick ` preceded by a space.
Source

Try the steps in the following link if you get the error Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.:
https://blog.darrenjrobinson.com/powershell-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send/

Converter curl to Invoke-WebRequest

I made a simple converter that takes a curl command and converts it to an equivalent Invoke-WebRequest command. It recognizes the following curl options:

An example curl command is already entered. The "More info" field shows that detected options.