Invoke webrequest on PowerSheel version 2

There was a requirement in my organization to fetch URL content using PowerShell. We do have Invoke-Webrequest already but it works on PowerShell version 3.0 or later. For earlier versions we can use assemblies to accomplish the same.

Below is the classic example for this.

Here I have stored the URL in a variable called $url.

Create a webclient and store in a variable called $webclient. Storing the object in a variable is useful if we want to set some parameters before executing the actual download operation.

Now we can easily download the content using DownloadString method and store it in new variable called $webpage.

After storing the content in this variable, we can match the content or specific string, if required.


There are many options you can use as per your requirement. For instance, what if you want to set the User-Agent string before initiating the download request. Here is what you can do for this-

Here I have used the User-Agent string for Internet Explorer 11. There are other User-Agent strings available that you can use. For compatibitliy view (with Internet Explorer 7) you can use this-

Or you can use your custom string, like this-

You can refer the Microsoft blog for more information regarding User-Agent string here. Also, you can find whole list of User-Agents here.

What else use can do with your $webclient. If you to authenticate using your current logged-in credentiatials then you can use UseDefaltCredentails property for this.

You can even pass set of credentials using Credentials property.

IsBusy property can be used to check if your webclient is still busy or not.

It will return the Boolean value which is True of False.

This is the Sample for C#

Here is the MSDN for

Leave a Reply

Your email address will not be published. Required fields are marked *