How can I extract the value of a specific input field from HTML in PowerShell?

June 4, 2025 1 answer
Asked by bake180c
I've been trying to extract the value associated with a specific input field named 'key' from the HTML on a webpage, but I'm not having any luck. I'm using the following PowerShell code to fetch the HTML content: ```powershell $url = 'https://myurl.com' $content = Invoke-WebRequest -Uri $url -Method get -UseDefaultCredentials Write-Host $content ``` The HTML structure I receive includes multiple `` elements within a `` tag. Specifically, I'm looking to retrieve the value for the 'key' input (which looks like this: ``). Can anyone guide me on how to do this in PowerShell?

Answers

one.way.ticket
You can easily extract input values using regex capture groups. Here's a quick snippet that should help: ```powershell $Result = $content | Select-String -Pattern '<input type="(.*?)" name="(.*?)" value="(.*?)"' -AllMatches foreach ($Match in $Result.Matches) { [pscustomobject]@{ InputType = $Match.Captures[0].Groups[1] Name = $Match.Captures[0].Groups[2] Value = $Match.Captures[0].Groups[3] } } ``` This code assumes the input fields maintain a consistent order, so make sure to adapt it if your HTML changes!

_salsa_steps_: Thanks! I noticed the value returned is trimmed with ellipsis. Do you know how to avoid that? I've tried a method to extract it, but it doesn't seem to be working right.

LANE CLOSED: Got it to work! I simplified my regex to just this and it works perfectly: ```powershell $Result = $content | Select-String -Pattern 'name="key" value="(.*?)"' -AllMatches foreach ($Match in $Result.Matches) { $Value = $Match.Captures[0].Groups[1] } ``` Super effective!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions