PowerShell is a powerful scripting language that provides the tools to automate various tasks on Windows systems. One of its handy features is the ability to send HTTP requests using the Invoke-WebRequest
cmdlet. In this blog post, we’ll explore using PowerShell and Invoke-WebRequest to send a HTTP Request, interact with web services, and retrieve data from websites.
Why Use PowerShell for HTTP Requests?
PowerShell’s Invoke-WebRequest
cmdlet is a versatile tool for sending HTTP requests. We can use it to:
- Retrieve data from web services or websites.
- Automate interactions with REST APIs.
- Monitor website availability and response times.
- Download files from the internet.
- Submit data to web forms.
Basic Usage of Sending a GET request using Invoke-WebRequest
You might want to refer to how to using PowerShell to read and write JSON for how to process the response.
But in this first example we use Invoke-WebRequest
to send a simple GET request to a web page, and view the response. We pass in a GET argument called “company” with a value of “alkane”.
Basic Usage of Sending a POST request using Invoke-WebRequest
If we need to send more complex data to a web service, we can use Invoke-WebRequest
to send POST requests too. In this example, since i prefer working with JSON, so we convert our $data
hashtable to JSON and send it as the -Body paramater. Note that in this case we must specify the -ContentType "application/json"
Error Handling
When sending HTTP requests, it’s important to handle errors gracefully. Unfortunately PowerShell’s Invoke-WebRequest
does a poor job of handling non-200 HTTP responses very well. So we need to use a try/catch block instead:
Handling Authentication
When interacting with web services that require (basic in this case) authentication, we can include credentials in our request:
Handling Headers
We can add custom headers to our requests as well. For instance, there we include headers called “Accept” and “company”:
PowerShell’s Invoke-WebRequest
cmdlet is a valuable tool for sending HTTP requests, making it easier to interact with web services, retrieve data, and automate tasks involving web resources. Whether we need to scrape data from websites, interact with REST APIs, or perform various web-related tasks, PowerShell can simplify the process.

