Use PowerShell to Download the HTML of a Website

By | 2024-04-11

You can use PowerShell’s Invoke-WebRequest cmdlet to download the HTML content of a website. This versatile cmdlet not only allows you to fetch files from the web via HTTP and HTTPS but also provides the capability to analyze and extract information from web pages.

Here are a few ways to achieve this.

Download HTML Content

– To simply download a webpage, use the following command:

Invoke-WebRequest -Uri "https://www.example.com" -OutFile "output.html"

Replace “https://www.example.com” with the actual URL of the website you want to download. The -OutFile parameter specifies the local file where the content will be saved.

Analyze Web Page Contents

When you use Invoke-WebRequest, it returns an object of type HtmlWebResponseObject. This object contains various properties that allow you to explore the web page’s details.

For instance, you can access the raw HTML code of the web page using:

$WebResponseObj = Invoke-WebRequest "https://www.example.com"
$RawHtmlContent = $WebResponseObj.Content

Additionally, properties like Links, ParsedHtml, and Images provide further insights into the page’s structure and elements. For example, if you want to display all the links found in the last example, simply type the following:

$WebResponseObj.links.href

Remember that PowerShell’s Invoke-WebRequest is a powerful tool for interacting with web content, and you can adapt it to your specific needs. Happy scripting!

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.