PowerShell:
Downloading a web page
How to:
Here’s the magic spell for fetching a web page using PowerShell. We’ll harness Invoke-WebRequest.
# Grab the content of example.com
$response = Invoke-WebRequest -Uri "http://example.com"
# Here's what you got
$response.ContentSample output:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
...
<!-- and so on -->
</head>
...
</html>You could be after just text, no HTML tags. Let’s do that:
# Just the text, please
$response.ParsedHtml.body.innerTextDeep Dive
Once upon a time, PowerShell didn’t have the cool Invoke-WebRequest cmdlet. Coders would use the .NET System.Net.WebClient class or resort to external tools. Now, it’s all built-in, simplifying tasks for us all.
Invoke-WebRequest offers more than just content. Headers, status, and session info – it’s all there. If you’re playing with APIs, you’ll love Invoke-RestMethod as a focused alternative.
Under the hood, these cmdlets rely on the heavyweight .NET HttpClient class, packing reliability and extensive functionality.
And, if you’re getting impatient waiting for that web page to download, Invoke-WebRequest supports asynchronous operations too. However, that’s a topic for another day.
See Also
- The Invoke-WebRequest documentation
- More about Invoke-RestMethod for API interactions
- A PowerShell GitHub repository for the curious coders who like to peek under the hood.