Categories

#PSTip Access web from PowerShell console

As I am still running PowerShell 2.0 on my work computer, I can’t use the Invoke-WebRequest cmdlet. This cmdlet is introduced in Windows PowerShell 3.0. When I need to download some web page, I still use my old .NET friend–Net.WebClient class.

The following command will download the Bing home page:

(New-Object Net.WebClient).DownloadString(‘http://www.bing.com’

As I use this class frequently, I’ve created a function for that and I am exposing a Net.WebClient object as a variable to my global scope.

As you can see I’ve set also some other properties to satisfy my needs:

Use of default credentials. As we user proxy with authentication, I need to pass my actual credentials to proxy.

Setting encoding to UTF8

I am not using cache.

At the end of New-WebClient function, I send the object out and assign it to a global $wc variable. I can use it later as in the following command:

$web = $wc.DownloadString('http://www.bing.com')

I know that some people uses this technique to process RSS channels. I don’t use it for this scenario, but you can download RSS channel, convert it to XML and then process in console. Frequent usage is:

PS> $rss = $wc.DownloadString('http://feeds.feedburner.com/PowershellMagazine')
PS> $rss.rss.channel.item | Format-Table Title
title
-----
Two new PowerShell modules related to Storage Spaces
#PSTip How to speed up the Test-Connection command
#PSTip Get system power information
#PSTip Wait for executable to finish
#PSTip Converting numbers to binary and back
Using SkyDrive to sync your WindowsPowerShell folder
#PSTip Get your reboot history
#PSTip Converting numbers to HEX
Connecting to Hyper-V virtual machines with PowerShell
Manipulating Wildcards

Like this:

About the author: David Moravec

I am in IT industry for 15 years, last 10 years working mainly with Microsoft management technologies.
I Work in one of the biggest international accounting companies as administrator responsible for System Center Configuration Manager. I like to automate my infrastructure-related tasks with windows PowerShell. Author and speaker for Microsoft Czech Republic. I'm Running my own blog at PowerShell.cz. You can reach me on Twitter at @makovec