Parse a web page and download all PDFs

Today I downloaded the great ISESteroids add-in for the PowerShell ISE. While I was on their website, I noticed that they have a collection of free PDF “cookbooks” available.

I didn’t want to save each file down manually, and liked the thought of using a PowerShell script to download PowerShell documents from a PowerShell-related website. Here’s what I did to download them all:

$psPage = Invoke-WebRequest "http://www.powertheshell.com/cookbooks/"
$urls = $psPage.ParsedHtml.getElementsByTagName("A") | ? {$_.href -like "*.pdf"} | Select-Object -ExpandProperty href

$urls | ForEach-Object {Invoke-WebRequest -Uri $_ -OutFile ($_ | Split-Path -Leaf)}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s