Computer >> Computer tutorials >  >> Programming >> C++

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

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

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/