Search Tools Links Login

Use PowerShell to Download the HTML of a Website


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!

About this post

Posted: 2024-04-11
By: dwirch
Viewed: 157 times

Categories

Scripting

Powershell

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.