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:
-O= Give the downloaded file the same name as the remote file-H "Bearer: AbCdEfGH-0123456789;"= Header sent with the request"https://example.com/downloads/app-x64-20651.zip"= The URL to the downloaded file
The equivalent Invoke-WebRequest parameters are (replace underlined parts with actual values):
-OutFile filename= Specify a name for the downloaded file-Headers @{'key1' = 'value1'; 'key2' = 'value2';}= Syntax for key-value pairs-Uri download-URL= The download URL
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'
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:
-O– Name the output file the same as the downloaded file. Has no practical effect with Invoke-WebRequest, because-OutFileis required to save to a file.-k– Insecure (don't check certificate). Converted to-SkipCertificateCheck, which is available in PowerShell 6.0 and above.-H– Zero or more headers. Must be wrapped in double quotes, like the example above.
An example curl command is already entered. The "More info" field shows that detected options.