Accessing the Internet from PowerShell: Net.WebClient

The first thing we need to understand in order to make PowerShell talk with web content, is a .Net framework object, WebClient. WebClient is found in the System.Net namespace and provides us, the humble PowerShell developer with a simplified method for accessing HTTP/HTTPS/FTP content. Yes, you did just read that correctly, not only will our code be able to make use of HTTP(S) based content but it will also allow us to hit FTP Servers as well!

So lets get a PowerShell window open, and get an instance of the WebClient object. Simply make a new-object of type Net.WebClient and put it in a variable!

Congratulations, you have completed the first step.

Now lets take a look at what the WebClient offers us:

WOAH, that is quite a few methods and properties there.

For now, as I am just introducing the WebClient to you, lets just try to download a single page. Lets put the URL we want into a variable, and then call DownloadString, with a parameter of the URL variable:

The output, if everything has worked and your internet is running, will start with something like:

What the hell? that doesn’t look like does in Internet Explorer! Yes, that's because we will be seeing the un-rendered HTML, so things will be a little less friendly.

That is it for todays lesson, try downloading some pages yourself and see how you go. The next lesson will cover setting up some optional settings for WebClient such as controlling proxy server access and other context information.