Powershell

Execute Curl From PowerShell

The cURL, an acronym of client URL, is a command-line tool and a library to transfer data to and from the server. The data sent is used to perform various actions, such as creating a file and deleting files. The cURL tool supports almost all well-known protocols, such as HTTP, HTTPS, and FTP. In most programming languages, cURL is used with the help of a built-in library. The cURL supports various Operating Systems, such as Windows, Linux, and macOS. Furthermore, it requires network connectivity and a command-line shell to transfer data over the server.

How Does cURL Work?

The cURL command is applied to make the requests to complete the tasks automatically, and the most used cURL task is to test the endpoints.

HTTP Method: HTTP supports various methods for various purposes. Such as “OPTIONS“, “TRACE“, and “PATCH“. However, few methods are used frequently, and they are described below:

GET: This method only reads data from the server, and it does not disturb the server’s state. If you do not specify the curl command’s process, it uses GET as a default method.

POST: This method contains the information that the server has to process, such as posting some message or creating files. POST consists of a body that includes the information to be sent to the server. Contrary to GET, it changes the state of the server by appending information.

PUT: This method is used to update or create a record in a database, or one can use this method to edit the content of any file. The PUT method sends data to any resource, and the server processes this information to perform a specific action on that resource.

DELETE: As the name is self-explanatory, this method deletes any resource, such as database entry. DELETE does not contain body structure as like POST and PUT do.

Endpoints: It is the address to which you are sending the request, and it is in the form of a URL.

Headers: They contain metadata related to the requests, such as the content type of the request.

Body: It is the message we require to send a request. The Body is used in the PUT and POST methods as PUT requires some data to create or delete a specific item. Similarly, POST also requires some data to send the information.

Using cURL With PowerShell

In this section, we will perform some basic cURL actions using PowerShell:

In Microsoft Windows PowerShell, a cmdlet Invoke-WebRequest can also be used as an Alias to curl. You can check it using the following command:

> Get-Command curl

How to Make Web Request Using the cURL Command in PowerShell

There are various ways to make a web request using the curl command:

You can use “curl” or “Invoke-WebRequest” to get the same result. As mentioned above, “Invoke-WebRequest” is an Alias of “curl“.

The command given below will retrieve data from the web address “www.google.com“:

> curl https://www.google.com

Moreover, you can write “curl” and press enter. The command-line PowerShell will ask you to enter the “Uri“:

Once you enter the web address, it will show the same output as in the above command:

One can use the following code to get the same content:

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

The above commands help get detailed information about a web page. However, if you want to get related “Content” only, you have to run the following command to get the content. For this, you have to join -ExpandProperty with pipe operator “|” as shown below:

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

Similarly, you can use -ExpandProperty to get the detailed content of any information extracted using curl .

How to Save Webpage Content to a File Using cURL in PowerShell:

If you want to save the content of the website to a specific file, then you must follow the command given below:

> curl https://www.google.com > curloutput.txt

This command will create a file named “curloutput.txt” and store the data retrieved from “www.google.com“.

Moreover, one can get the file’s content in an output file using the “-O” flag. The “-O” flag is used to keep the output in file “output1.txt“. The following command will help to copy the content to a file named “output1.txt“:

> curl -O output1.txt https://www.google.com

How to Get Webpage Links Using cURL in PowerShell:

With the help of curl/Invoke-WebRequest, you can get the links on the webpage. Then, copy and paste the following command to get the links on “www.youtube.com“:

> (Invoke-WebRequest -Uri "https://www.youtube.com").Links.Href

Conclusion:

In this current era of computing, various operating systems provide multiple ways for their command line terminal to automate the tasks. A cURL is a command-line tool that automates your search for webpage requests. It supports various operating systems, such as Linux, macOS, and Windows. Moreover, its primary use is to send and receive the data from the server.

In this post, a detailed description of cURL is provided concerning its application in PowerShell. The article highlights the basic knowledge of cURL, followed by its basic working, and concluded with its application in PowerShell.

About the author

Adnan Shabbir