Invoke-WebRequest
is a powerful cmdlet in PowerShell for sending HTTP or HTTPS requests to web servers and receiving responses. It can handle various HTTP methods (GET, POST, PUT, DELETE) and is commonly used for downloading files, interacting with APIs, and scraping websites.
Basic Syntax
The basic structure of using Invoke-WebRequest
is:
Invoke-WebRequest -Uri "http://example.com"
You can customize the request using parameters such as -Method
, -Headers
, and -Body
for different web interactions.
Downloading Files with Invoke-WebRequest
A key feature of Invoke-WebRequest
is its ability to download files. The -OutFile
parameter lets you specify where to save the downloaded file.
Example: Downloading a File
To download a file, use the following command:
Invoke-WebRequest -Uri "http://example.com/file.zip" -OutFile "C:\path\to\save\file.zip"
-Uri
: Specifies the URL of the file you want to download.-OutFile
: Defines the local file path where the downloaded content will be saved.
This command will download the file from the given URL and save it to your local system.
Example: Downloading a File with Progress Indicator
When downloading large files, PowerShell will automatically show a progress bar:
Invoke-WebRequest -Uri "http://example.com/largefile.zip" -OutFile "C:\path\to\save\largefile.zip"
You’ll see the download progress in the terminal as it happens.
Sending HTTP Requests (GET, POST, etc.)
Invoke-WebRequest
supports several HTTP methods, including GET, POST, PUT, and DELETE, allowing for flexible web interactions, such as retrieving data from websites, submitting forms, or interacting with APIs.
Example: GET Request
A GET request is used to retrieve data from a server. Here’s an example to fetch the content of a webpage:
$response = Invoke-WebRequest -Uri "http://example.com"
Write-Host $response.Content
$response.Content
: Contains the HTML or other data returned by the server.
Example: POST Request
To send data to a server (e.g., submitting a form or making an API call), you can use the POST method. Here’s how to send JSON data:
$headers = @{ "Content-Type" = "application/json" }
$body = '{"username":"user1", "password":"password123"}'
$response = Invoke-WebRequest -Uri "http://api.example.com/login" -Method POST -Headers $headers -Body $body
Write-Host $response.Content
-Method POST
: Sends data to the server.-Body
: Contains the data being sent (in this case, JSON).-Headers
: Defines the request headers (such asContent-Type
).
Working with $response.Content
The $response.Content
property contains the body of the HTTP response, and how you handle it depends on the type of content returned. Below are different ways to work with it.
1. Viewing and Handling Text-Based Content (HTML, JSON, etc.)
When the response is text-based (HTML, JSON, etc.), you can display or parse the content.
Example: Displaying HTML Content
If the content is HTML (common for web pages), you can directly display it in the console:
$response = Invoke-WebRequest -Uri "http://example.com"
Write-Host $response.Content
Example: Parsing JSON Data
If the response is JSON (such as from an API), use ConvertFrom-Json
to parse it into a PowerShell object:
$response = Invoke-WebRequest -Uri "http://api.example.com/data"
$data = $response.Content | ConvertFrom-Json
Write-Host $data.property
ConvertFrom-Json
: Converts the JSON string into a PowerShell object for easy manipulation.
2. Extracting Data from HTML Content
If the response content is HTML and you need to extract specific data (such as links or titles), you can use regular expressions or PowerShell’s HTML parsing features.
Example: Extracting Links from HTML
To extract all the hyperlinks (href
attributes) from a webpage:
$response = Invoke-WebRequest -Uri "http://example.com"
$matches = [regex]::Matches($response.Content, 'href="([^"]+)"')
foreach ($match in $matches) {
Write-Host $match.Groups[1].Value
}
This will output all the links found on the webpage.
Example: Extracting the Title from HTML
You can also use PowerShell’s HTML parsing capabilities to extract specific tags:
$response = Invoke-WebRequest -Uri "http://example.com"
$html = $response.ParsedHtml
$title = $html.getElementsByTagName("title")[0].innerText
Write-Host $title
This extracts the content of the <title>
tag from the webpage.
Key Parameters for Invoke-WebRequest
Parameter | Description |
---|---|
-Uri | The web address to send the request to. |
-Method | The HTTP method (GET, POST, PUT, DELETE, etc.). |
-Headers | Custom headers for the request (e.g., Content-Type). |
-Body | Data to include in the request body (used with POST, PUT). |
-OutFile | The location to save the response content (useful for file downloads). |
-TimeoutSec | Time limit for the request to complete (in seconds). |