PowerShell: Invoke-WebRequest and URL links

I was working on a project recently that needed to count the number of links on a given URL. With the code complete I wanted a simple way to validate my work to make sure my totals matched. So, I turned to Windows PowerShell for a quick test and to continue my learning of this powerful tool.

PowerShell 3.0 introduced the Invoke-WebRequest cmdlet that makes it easy to get content from a URI passed in as a parameter. The cmdlet goes out to the internet and returns collections of forms, links, images, and other significant HTML elements from a web page.

To run the command all you need is a webpage address as a parameter:


From the image above you can see there is a lot of information returned. We can see the StatusCode, Content, Forms, etc. Each of these properties returned is also available for further inspection. To see a full list we can use the Get-Member cmdlet to retrieve them all. The cmdlet also allows for the “gm” alias to be used.

Invoke-WebRequest -Uri 'http://www.lego.com/en-us/' | gm -MemberType Property

We can see from the list below that all of the properties returned matches the content in the image above:

Invoke-WebRequest Properties

From the list I see the Links property is available which is exactly what I need for my code validation. However, just adding .”Links” to my Invoke-WebRequest command will only return the hyperlinks themselves. This might be all you need if you want to ensure a returned webpage contains a link you are looking for. However, since I need a sum of links I will append the Count method so I can see the final total.

Invoke-WebRequest URL Link Count

To see a parameterized version of this script visit my . It can also be found in the TechNet Gallery under Count webpage links within a given URL (PowerShell) at https://gallery.technet.microsoft.com/Count-webpage-links-within-6c00db86.

If you have solved any problems with a cool PowerShell hack please leave a comment below.

Related Articles:

Publishing scripts to TechNet


About Ken Cenerelli
I am a Programmer Writer, .NET Developer, Microsoft MVP - Visual Studio and Development Technologies, Public Speaker, Blogger, and Microsoft Azure Nerd. I blog regularly at kencenerelli.wordpress.com and can be found on Twitter via @KenCenerelli.

3 Responses to PowerShell: Invoke-WebRequest and URL links

  1. Pingback: List Services With PowerShell | Ken Cenerelli

  2. Pingback: 2015 year in review | Ken Cenerelli

  3. Pingback: 2016 Year in Review | Ken Cenerelli

%d bloggers like this: