Is there an equivalent of curl in PowerShell? Does it have some similar built-in capability or is there a 3rd party cmdlet?
- 26,555
- 15
- 113
- 235
- 14,048
- 32
- 81
- 93
-
5Closed? How about closing http://superuser.com/questions/34492/powershell-equivalent-to-unix-which-command, http://superuser.com/questions/295021/windows-powershell-equivalent-to-unix-linux-pwd, http://superuser.com/questions/71446/equivalent-of-bashs-source-command-in-powershell and host of others? – Borek Bernard Oct 10 '11 at 12:39
-
1Actually this question looks fair enough to me? But there's an old answer: http://stackoverflow.com/questions/340553/what-is-the-best-way-to-send-http-requests-from-windows-powershell – Rup Oct 10 '11 at 12:49
-
1Check out this article about using curl in PowerShell: http://thesociablegeek.com/azure/using-curl-in-powershell/ – SDsolar Oct 16 '17 at 03:12
9 Answers
PowerShell 3.0 has the new command Invoke-RestMethod:
http://technet.microsoft.com/en-us/library/hh849971.aspx
more detail:
https://discoposse.com/2012/06/30/powershell-invoke-restmethod-putting-the-curl-in-your-shell/
- 231
- 3
- 11
- 1,575
- 1
- 10
- 5
-
10You may want the Invoke-WebRequest command instead, depending on what you are trying to accomplish. – Timothy Lee Russell Jun 10 '14 at 16:32
-
44
-
44Yeah it's bizarre that they'd alias them, as the syntax is entirely different. If MS doesn't want to ship a package manager and make it easy to get common, basic tools, hiding it behind a fake alias isn't gonna make the situation better. – MichaelGG Nov 07 '14 at 20:08
-
3The biggest problem with Invoke-WebRequest is that it does not work if IE is not 'initialized'; so when writing pipeline or DSC scripts you have to do some extra legwork first or you can use Invoke-RestMethod. – LimpingNinja May 22 '17 at 15:27
-
1@LimpingNinja, do you have a source for this? Maybe the problem you experienced was due to this: "-UseBasicParsing Uses the response object for HTML content without Document Object Model (DOM) parsing. This parameter is required when Internet Explorer is not installed on the computers, such as on a Server Core installation of a Windows Server operating system." from https://msdn.microsoft.com/powershell/reference/3.0/microsoft.powershell.utility/Invoke-WebRequest – Michael Kelley May 22 '17 at 19:24
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:
- 951
- 6
- 6
-
What's the -Uri flag doing... I get same results with or without it for 'curl -Uri https://api.github.com/rate_limit'. I've searched for this, but still not sure – Drenai Jul 28 '17 at 08:35
-
2In powershell some parameter names can be implicitly derived from the order of the parameter values. You can see this through looking at `get-help curl`. You'll see `Invoke-WebRequest [-Uri]
...`, with the brackets `[]` indicating `-Uri` can be left out (and therefore implicitly invoked). As I mentioned in the main post: in general you ought be explicit (where you or others are going to read your code in the future). – John Bentley Jul 30 '17 at 22:59 -
Ah - thanks. So I'm leaving it out, but it's still being invoked, didn't know that's what the ```[]``` did – Drenai Jul 31 '17 at 10:22
-
You are welcome. You've got it. The syntax descriptions for powershell commands that you find in `get-help` is given in some version of https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form. You'll do well to read/skim that because some version of Extended Backus Naur Form is likely used to describe the syntax of many languages you'll come across. You'll notice that at https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form#Table_of_symbols brackets `[ ...]` designate syntax that is optional. – John Bentley Aug 01 '17 at 10:55
-
It is, but I suggest not using it, as it is confusing. On PowerShell Core 6.1.3 for Linux, neither curl nor wget are the aliases. They refer to the original curl and wget (if they are present in the system). Since they are different, usage of those aliases is a potential source of confusion and non-multiplatform scripts. – v6ak Mar 07 '19 at 16:04
-
@v6ak. On Powershell Core 6.1.3 for Linux are the problems you mention avoided if you use the non-aliased command `Invoke-WebRequest`? – John Bentley Mar 11 '19 at 01:49
-
-
My question was not whether `Invoke-WebRequest` exists on Powershell Core 6.1.3 but whether, on that platform, it avoids the problems you mention. That is, on Powershell Core 6.1.3 does `Invoke-WebRequest` avoid using the original curl if present on the system? – John Bentley Mar 11 '19 at 23:00
-
@JohnBentley Yes, Invoke-WebRequest (and alias iwr) are there and seem to behave the same as on Windows. The curl and wget seem to be skipped completely (Get-Alias does not show them, so, they are likely not shaded by /usr/bin/{curl,wget}). Maybe it is some tradeoff for request for complete removal of those aliases: https://github.com/PowerShell/PowerShell/pull/1901 . – v6ak Mar 12 '19 at 16:01
-
The PR you link to ultimately led to [the decision to reject the removal of curl and wget aliases in Powershell](https://github.com/PowerShell/PowerShell-RFC/blob/master/X-Rejected/RFC0007-Weak-Aliases.md#powershell-committee-decision). So the workaround seems to be: if you have curl separately installed on your system use (the non aliased) Invoke-WebRequest if you want to invoke that Powershell installation/implementation and use `curl` if you want to invoke your curl installation/implementation The decision includes the general recommendation to not rely on aliases. – John Bentley Mar 14 '19 at 03:09
-
@JohnBentley Yes, the PR itself has been rejected. But it looks like the change has been done for PS Core, at least on Linux. So, there might have been some followup discussion on it. (BTW, without @-mention, I don't get notifications about your reply.) – v6ak Mar 31 '19 at 11:02
-
@v6ak. Yes, it looks like an ongoing issue. Thanks for flagging the @-mention to me. – John Bentley Apr 03 '19 at 01:01
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"
- 429
- 6
- 12
- 497
- 1
- 4
- 7
-
[This answer](http://superuser.com/q/25538/294830) mentions the `DownloadFile` function which works well too. – Paul Hicks May 19 '16 at 22:36
You can install cURL with Chocolatey and have curl available in PowerShell CLI or cmd.
- 451
- 6
- 15
-
Then go vote for this https://connect.microsoft.com/PowerShell/feedbackdetail/view/1089296/remove-curl-and-wget-aliases-for-invoke-webrequest – MotoWilliams Jan 15 '15 at 23:46
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)%\git\bin in your PATH. Then you'll be able to use the curl command from the Windows Command Prompt or PowerShell console.
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.
On PowerShell you need to run explicitly curl.exe if curl is an existing alias
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
- 111
- 1
- 2
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.
- 24,736
- 27
- 129
- 199