Ed The Dev .com

Edward Delaporte's Technical Journal

Get-Webpage in PowerShell

no comment

I love how fast and yet flexible PowerShell is for many quick tasks; but sometimes doing something relatively simple has too high a learning curve to make PowerShell a practical choice. Getting the contents of a web-page is one of those times; so here I have provided a simple snippet that grabs the source code from a web-page of your choice. Now you can get to doing what you actually need to get done with the page contents, using PowerShell. Enjoy! - Edward


function Get-Webpage( [string] $page )
{
   $client = new-object system.net.WebClient
   $client.Headers.Add("user-agent", "PowerShell")
   $data = $client.OpenRead($page)
   $reader = new-object System.IO.StreamReader($data)
   [string] $s = $reader.ReadToEnd()
   $data.Close()
   $reader.Close()
   return $s
}

Get-Webpage("http://www.edthedev.com")

Leave a Reply

You must be logged in to post a comment.