[Solved] PowerShell equivalent of curl

Is there an equivalent of curl in PowerShell? Does it have some similar built-in capability or is there a 3rd party cmdlet?

Solution #2:

As of Powershell 5.0, if not before, curl is an alias for Invoke-WebRequest.

PS> Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize

CommandType Name                      Version Source
----------- ----                      ------- ------
Alias       curl -> Invoke-WebRequest
Alias       iwr -> Invoke-WebRequest
Alias       wget -> Invoke-WebRequest

To use the unaliased command …

PS> Invoke-WebRequest -Uri https://localhost:443/
PS> Invoke-WebRequest -Uri https://www.google.com

So return several properties of the request as follows …

PS> Invoke-WebRequest -Uri https://www.google.com

StatusCode        : 200
StatusDescription : OK
Content           : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-AU"><head><meta content="text/html; charset=UTF-8"
                    http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/...
RawContent        : HTTP/1.1 200 OK
                    X-XSS-Protection: 1; mode=block
                    X-Frame-Options: SAMEORIGIN
                    Vary: Accept-Encoding

… or just the content …

PS> Invoke-WebRequest -Uri https://www.google.com | Select-Object -ExpandProperty Content

<!doctype html><html itemscope="" itemtype="http://schem[etc ...]

The equivalent aliased commands are …

PS> curl -Uri https://www.google.com
PS> curl -Uri https://www.google.com | Select-Object -ExpandProperty Content

Leveraging Powershell defaults and other aliases you could shorten the commands to

PS> curl https://www.google.com 
ps> curl https://www.google.com | Select -ExpandProperty Content

… but I wouldn’t recommend it. Verbose commands help others when reading your code.

Update:

Powershell 6.x

Use of Aliases discouraged

As of Powershell 6.x “Core” curl is no longer an alias for Invoke-WebRequest (the alias wget is also removed) . Instead use Invoke-WebRequest directly.

PS> Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize

CommandType Name                     Version Source
----------- ----                     ------- ------
Alias       iwr -> Invoke-WebRequest

Curl is no longer an alias for Invoke-WebRequest (tested on Powershell 6.2.3), despite an apparent rejection of a motion in an RFC “to to remove the aliases curl and wget from Windows PowerShell”.

That RFC notes “The wget/curl aliases were already removed from PowerShell Core so the problem [of having those aliases] was limited to Windows PowerShell.”

In the conclusion the Powershell team also encourages users “to not rely on aliases in scripts”.

As @v6ak has noted in the comments using curl and wget in PowerShell (5.0 or lower) can be a problem in: unintentionally invoking the real curl or wget if installed side-by-side; and, in any case, causes confusion.

New Encoding

It is recommended you upgrade Powershell “core” (6.x or above) in order to take advantage of the default encoding utf8NoBOM, when using Invoke-WebRequest (and many other text outputting commands). If one was doing this explicitly you could do something like:

Invoke-WebRequest ` 
-Uri https://raw.githubusercontent.com/fancyapps/fancybox/master/dist/jquery.fancybox.min.js `
| Select-Object -ExpandProperty Content `
| Out-File jquery.fancybox.min.js `
-Encoding utf8NoBOM 

However, even when using a shorter, implicit, command …

Invoke-WebRequest `
-Uri https://raw.githubusercontent.com/fancyapps/fancybox/master/dist/jquery.fancybox.min.js `
-OutFile jquery.fancybox.min.js

… encoding with utf8NoBOM will be done (you can verify this, for example, by opening the saved file in Visual Studio Code and observing “UTF-8” in the status bar).

Files saved with utf8NoBOM tend to cause fewer problems when traveling through various ecosystems. Of course, if you need some other encoding you can set some alternative explicitly.

In Powershell 5.0 and lower the utf8NoBOM encoding was not available, let alone the default.

Details:

Respondent: John Bentley

Solution #3:

The excellent Command Line Kung Fu blog has a post where they compare curl, wget and the related PowerShell commands

In a nutshell:

(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html","C:hello-world.html")

Or, if your version of Powershell/.Net doesn’t accept 2 parameters for DownloadString:

(New-Object System.Net.WebClient).DownloadString("http://www.example.com/hello-world.html") > "C:hello-world.html"
Respondent: ipr101

Solution #4:

You can also install Git for Windows, and then put the Git bin folder in your path. The Git install includes, among other things, curl.exe. After installing, just put %programfiles(x86)%gitin in your PATH. Then you’ll be able to use the curl command from the Windows Command Prompt or PowerShell console.

Respondent: Jamie

Solution #5:

You can install cURL with Chocolatey and have curl available in PowerShell CLI or cmd.

Respondent: Spoike

Solution #6:

If you use insider preview build 17063 or release version 1803 and up then there’s already curl.exe that you can run directly

If you’re one of these people – HAPPY NEW YEAR! ? Windows 10 Insider build 17063 and later now include the real-deal curl and tar executables that you can execute directly from Cmd or PowerShell.

Tar and Curl Come to Windows!

On PowerShell you need to run explicitly curl.exe if curl is an existing alias

Respondent: phuclv

Solution #7:

the closest thing to wget or curl on windows is bits (Background Intelligent Transfer Service), which has some snippets ready for powershell.

Respondent: akira

Solution #8:

curl

cmd, bash

curl -H "Host: whoami.local" 192.168.4.4

powershell

Invoke-WebRequest -H @{"Host"="whoami.local"} -UseBasicParsing 192.168.4.4

# or 
# $(Get-Command curl) return  "curl -> Invoke-WebRequest"
curl -H @{"Host"="whoami.local"} -UseBasicParsing 192.168.4.4
Respondent: Archon

Solution #9:

This command should work:

Invoke-WebRequest -UseBasicParsing -Uri http://example.com/

It’s part of Microsoft.PowerShell.Utility since PowerShell 3.0.

See also: Get $webclient.downloadstring to write to text file in Powershell.

Respondent: kenorb

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

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

New ride at Perth's Adventure World a ‘swing on steroids’ - Australasian Leisure Management npp pharma the truth about anabolic steroid drugs and professional sports