115

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

migrated from stackoverflow.com Oct 10 '11 at 11:36

This question came from our site for professional and enthusiast programmers.

77

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/

  • 7
    You may want the Invoke-WebRequest command instead, depending on what you are trying to accomplish. – Timothy Lee Russell Jun 10 '14 at 16:32
  • 22
    It's also aliased as curl or wget in Powershell now. – CMCDragonkai Jun 12 '14 at 5:49
  • 10
    Yeah 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 7 '14 at 20:08
  • The 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
  • @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 msdn.microsoft.com/powershell/reference/3.0/… – Michael Kelley May 22 '17 at 19:24
27

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"
  • This answer mentions the DownloadFile function which works well too. – Paul Hicks May 19 '16 at 22:36
26

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.

  • What's the -Uri flag doing... I get same results with or without it for 'curl -Uri api.github.com/rate_limit'. I've searched for this, but still not sure – Drenai Jul 28 '17 at 8:35
  • 1
    In 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] <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 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 en.wikipedia.org/wiki/… brackets [ ...] designate syntax that is optional. – John Bentley Aug 1 '17 at 10:55
15

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.

13

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

1

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

  • 2
    I think BITS solves a different problem than wget or curl. – ongle Jul 7 '12 at 1:08
  • Fetching things from a Http-Server? – akira Jul 7 '12 at 19:03
  • 2
    No :), the background intelligence part of it. – ongle Jul 7 '12 at 20:03
0

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.