powershell select-string not working correctly
You don't get the result you expect, because Select-String
doesn't output strings, but MatchInfo
objects. If you pipe the output of your first Select-String
into the Get-Member
or Format-List
cmdlet, you'll get something like this:
PS C:\> bcdedit /enum | Select-String "identifier.*current" -Context 0,3 | Get-Member TypeName: Microsoft.PowerShell.Commands.MatchInfoName MemberType Definition---- ---------- ----------Equals Method bool Equals(System.Object obj)GetHashCode Method int GetHashCode()GetType Method type GetType()RelativePath Method string RelativePath(string directory)ToString Method string ToString(), string ToString(string directory)Context Property Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}Filename Property string Filename {get;}IgnoreCase Property bool IgnoreCase {get;set;}Line Property string Line {get;set;}LineNumber Property int LineNumber {get;set;}Matches Property System.Text.RegularExpressions.Match[] Matches {get;set;}Path Property string Path {get;set;}Pattern Property string Pattern {get;set;}PS C:\> bcdedit /enum | Select-String "identifier.*current" -Context 0,3 | Format-List *IgnoreCase : TrueLineNumber : 17Line : identifier {current}Filename : InputStreamPath : InputStreamPattern : identifier.*currentContext : Microsoft.PowerShell.Commands.MatchInfoContextMatches : {identifier {current}
The Line
property contains the actual matching line, and the Context
property contains child properties with the pre- and post-context. Since the description
line you're looking for is in the PostContext
child property, you need something like this for extracting that line:
bcdedit /enum | Select-String "identifier.*current" -Context 0,3 | Select-Object -Expand Context | Select-Object -Expand PostContext | Select-String 'description'
Bottom line: Select-String
does work correctly. It just doesn't work the way you expect.
Select-String
returns MatchInfo
objects, not just the string data displayed. That data is taken from the Line
and Context
properties of the MatchInfo
object.
Try this:
bcdedit /enum | select-string "identifier.*current" -context 0,3 | format-list
And you'll see the various properties of the MatchInfo
object.
Note that the Context
property is displayed as Microsoft.PowerShell.Commands.MatchInfoContextYou'll need to drill down into this object further to get more information:
(bcdedit /enum | select-string "identifier.*current" -context 0,3).context | format-list
There you'll see that the context
property is another object with PreContext
and PostContext
properties, where the actual Pre and PostContext lines are.
So:
(bcdedit /enum | select-string "identifier.*current" -context 0,3).Context.PostContext | Select-String 'description'
Will get the description line from the postcontext matches.
Or you can do this:
[string](bcdedit /enum | select-string "identifier.*current" -context 0,3) -split "`n" -match 'description'