Run Curl Command in PowerShell

Run Curl Command in PowerShell

In PowerShell 3.0 and higher, we have Invoke-WebRequest and Invoke-RestMethod cmdlets; curl is an alias of the Invoke-WebRequest in PowerShell. Most learners make it confused with the Invoke-RestMethod cmdlet. In PowerShell 7 and higher, the Invoke-RestMethod cmdlet is the default cmdlet for sending RESTful web service requests.

It is another built-in cmdlet in PowerShell mainly designed for working with RESTful APIs and web services. It makes sending and handling web requests simple by automatically parsing the response as XML or JSON and supports basic OAuth authentication and basic authentication.

However, the Invoke-WebRequest and curl are still available in PowerShell, which sends HTTPS and HTTP requests to the APIs and web servers. The Invoke-WebReqest cmdlet provides more flexibility than the Invoke-RestMethod cmdlet in handling non-HTTP protocols and customizing requests.

Do you still need clarification? Want to check the aliases of the commands? You can use the Get-Alias cmdlet to get all the aliases with respective names, versions, and sources in PowerShell. However, Get-Comman curl will only show the alias with the respective command, version and source.

So, using native PowerShell cmdlets will be more appropriate than using aliases. However, we will learn both (Invoke-WebRequest and curl) and let you choose based on your requirements.

Using Invoke-WebRequest Cmdlet

Use the Invoke-WebRequest cmdlet to retrieve data from the specified URL in PowerShell.

We used the Invoke-WebRequest cmdlet with the -Uri parameter to get contents from the specified web-page on the internet. The -Uri parameter was used to specify the Uniform Resource Identifier (URI) of the internet resource (web service or page) to which a web request was sent.

Note that the -Uri parameter only supports HTTP and HTTPS. Therefore, using this parameter is mandatory, but the parameter name -Uri is optional, which means we can specify the URL of a web page or service without using the -Uri parameter name.

Returning to the Invoke-WebRequest cmdlet, it sends HTTP and HTTPS requests to the given web page or service, parses the response and returns the collection of images, links and other essential HTML elements.

You can see the above output as a demonstration; we got StatusCode, StatusDescription, Content, RawContent, and other important information. We can also store this response in a variable and retrieve specific information using dot notation. For instance, in the following code, we used dot notation to access the Content property of the response.

Alternatively, we can use Invoke-WebRequest as iwr to avoid keystrokes. Using iwr will not affect the results; we will still get the same output.

Using curl Command

Use the curl cmdlet to retrieve data from the specified URL in PowerShell.

See, we got the same output as the Invoke-WebRequest -Uri "https://www.example.com" command returned because curl is the alias of the Invoke-WebRequest. So, again, we can store the response in a variable and access its Content property using dot notation as follows, which also must be the same results as we received while using Invoke-WebRequest.

That’s all about how to run curl command in PowerShell.

Was this post helpful?

Leave a Reply

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