If you mean the surrounding context lines, there's some stuff in the return object to help you (via .NET).
Each result of Select-String will have it's .Matches property. This is an array of [System.Text.RegularExpression.Match[]].
Each match will have various properties and methods. .Result() is incredibly useful, but unintuitively named. It allows you to perform a substition on the result. This document describes all of the substitutions you can make. I'll draw your attention to two: $` and $',
$Response2.RawContent -split "`n" | Select-String $ans | Select-Object -ExpandProperty Matches | Foreach-Object {
$allTextBefore = $_.Result("$`")
$allTextAfter = $_.Result("$'")
}
In case the names of the variables above do not make it clear, those variables will contain all the text before and after the result.
If you wanted to go back N number of lines, you could:
- Use -split to break each result into a number of lines
-split '(?>\r\n|\n)'
- Use negative indexing to get the last N lines, i.e.
$lines[-4..-1]