How to get website links using Invoke-WebRequest in PowerShell?


To get the links present on the website using PowerShell, we can first retrieve the data from the webpage using the Invoke-WebRequest cmdlet.

$req = Invoke-WebRequest -uri "https://theautomationcode.com"
$req

Output

To retrieve only links we can use that property and there you will also find some sub-properties like InnerHTML, Innertext, href, etc as shown in the output.

$req = Invoke-WebRequest -uri "https://theautomationcode.com"
$req.Links

Output

innerHTML : Scripts
innerText : Scripts
outerHTML : <A href="https://theautomationcode.com/scripts/">Scripts</A>
outerText : Scripts
tagName   : A
href      : https://theautomationcode.com/scripts/  

We need only links so we will use the href property.

$req.Links | Select -ExpandProperty href

Output

https://theautomationcode.com/2020/11/
https://theautomationcode.com/author/chiragce17/
https://theautomationcode.com/category/powershell/
https://theautomationcode.com/category/troubleshooting/

Updated on: 18-Jan-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements