Table of Contents
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.
1 2 3 |
Invoke-WebRequest -Uri "https://www.example.com" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
StatusCode : 200 StatusDescription : OK Content : <title>Example Domain</title> <meta charset="utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="viewport" conten... rawcontent : http ok age: vary: accept-encoding x-cache: hit content-length: cache-control: max-age="604800" content-type: text charset="UTF-8" date: sat apr gmt exp... forms headers images inputfields links information... innertext="More" outerhtml="<A" href="https://www.iana.org/domains/example">More information...; outerText=More information...; tagName=A; href=https://www.iana.org/domains/example}} ParsedHtml : mshtml.HTMLDocumentClass RawContentLength : 1256 |
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.
1 2 3 4 |
$response = Invoke-WebRequest -Uri "https://www.example.com" $response.Content |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<title>Example Domain</title> <meta charset="utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> |
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.
1 2 3 |
curl "https://www.example.com" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
StatusCode : 200 StatusDescription : OK Content : <title>Example Domain</title> <meta charset="utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="viewport" conten... rawcontent : http ok age: vary: accept-encoding x-cache: hit content-length: cache-control: max-age="604800" content-type: text charset="UTF-8" date: sat apr gmt exp... forms headers images inputfields links information... innertext="More" outerhtml="<A" href="https://www.iana.org/domains/example">More information...; outerText=More information...; tagName=A; href=https://www.iana.org/domains/example}} ParsedHtml : mshtml.HTMLDocumentClass RawContentLength : 1256 |
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
.
1 2 3 4 |
$response = curl "https://www.example.com" $response.Content |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<title>Example Domain</title> <meta charset="utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> |
That’s all about how to run curl command in PowerShell.