18

I was testing some nodejs server code and wanted to test the urls from windows power shell using the curl command. Some things to note here: 1. I have mingw and git bash installed in my system, and curl works fine from normal command prompt and git bash prompt. 2. Additionally I have downloaded the latest version of cURL from curl official web site and added the path of bin directory to the system's PATH variable.

But it seems like it is no good for power shell. However, powershell still recognizes cURL command regardless of additional cURL program or git bash present in the system or not. But it does not work as I expected it to be, for example I have tried the following command and:

Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.

PS C:\Users\Zobayer Hasan> curl -X GET -i localhost:8080
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'X'.
At line:1 char:6
+ curl -X GET -i localhost:8080
+      ~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

PS C:\Users\Zobayer Hasan> curl GET -i localhost:8080
curl : Cannot find drive. A drive with the name 'localhost' does not exist.
At line:1 char:1
+ curl GET -i localhost:8080
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (localhost:String) [Invoke-WebRequest], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

PS C:\Users\Zobayer Hasan> curl GET -i http://localhost:8080
curl : Cannot find drive. A drive with the name 'http' does not exist.
At line:1 char:1
+ curl GET -i http://localhost:8080
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (http:String) [Invoke-WebRequest], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

PS C:\Users\Zobayer Hasan>

But when I try it in normal command prompt, it works fine:

C:\Users\Zobayer Hasan>curl -X GET -i localhost:8080
HTTP/1.1 200 OK
Date: Wed, 31 Dec 2014 09:35:31 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Server connection was called
C:\Users\Zobayer Hasan>

I also get the same output when I try it from git bash.

My question here is what do I need to do if I want everything to run smooth in windows power shell as well? I have done a little bit of searching on the net, and could not find much helpful resources. Also I have not any prior experience with windows powershell. Usually use linux as development environment.

Zobayer Hasan
  • 2,187
  • 6
  • 22
  • 38

2 Answers2

52

It looks like you already have curl as an alias to Ivoke-WebRequest:

Invoke-WebRequest : A parameter cannot be found that matches parameter name 'X'.

Try running remove-item alias:\curl and see if you now get the right curl being invoked. Alternatively, try by specifying the absolute path, i.e. c:\curl\curl.exe ....

anthonyserious
  • 1,758
  • 17
  • 15
  • Thanks @anthonyserious, it solved my problem. But how did the alias get set in the first place. I don't remember doing anything with powershell before that, was working on a clean pc. – Zobayer Hasan Jan 01 '15 at 17:33
  • Thanks. On W10 1809 it is bundled with the OS, but as to how the alias got set in the first place, from [this answer](https://stackoverflow.com/a/25045154/2128797) it looks like MS pinched the alias to use with its own [Invoke-WebRequest](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6). – Laurie Stearn Sep 02 '19 at 10:03
7

Since I updated to PowerShell 4.0, an Alias was created to point to Invoke-WebRequest with the name of curl, so what I've done is I added my own alias to the curl.exe executable I want to use instead:

set-alias mcurl "C:\cygwin\bin\curl.exe"

Powershell 4.0

Get-Alias curl

CommandType Name ModuleName ----------- ---- ---------- Alias curl -> Invoke-WebRequest

My new Alias:

Get-Alias mcurl

CommandType Name ModuleName ----------- ---- ---------- Alias mcurl -> curl.exe

yaxzone
  • 347
  • 1
  • 3
  • Thanks @yaxzone it's good to know how to set custom aliases. I wasn't familiar with these at all, learnt something new. – Zobayer Hasan Jan 01 '15 at 17:37