Select-String
, when given input other than strings, uses simple .ToString()
stringification[1] on each input object before looking for the given pattern.
In your case, the [Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage]
instances output by Get-AppXPackage
stringify to the full package names (e.g., Microsoft.MicrosoftEdge_44.18362.387.0_neutral__8wekyb3d8bbwe
), which explains your output.
In order to make Select-String
search the for-display string representations of objects - as they would print to the console and as they would appear in a file saved to with >
/ Out-File
(cat
is Out-File
's built-in alias on Windows) - you must, surprisingly, use Out-String -Stream
as an intermediate pipeline segment:
Get-AppxPackage | Out-String -Stream | Select-String -Pattern 'edge' -Context 3, 3
Out-String
uses PowerShell's formatting system to produce human-friendly display representations of the input objects, the same way that default console output, the Format-*
cmdlets, and >
/ Out-File
do.
-Stream
causes the output lines to be sent through the pipeline one by one.
Given that the solution is both non-obvious and cumbersome, it would be nice if Select-String
directly supported this behavior, ideally by default, but at least on an opt-in basis via a switch parameter - see feature request #10726 on GitHub - up-vote the proposal there if you agree.
[1] More accurately, .psobject.ToString()
is called, either as-is, or - if the object's ToString
method supports an IFormatProvider
-typed argument - as .psobject.ToString([cultureinfo]::InvariantCulture)
so as to obtain a culture-invariant representation - see this answer for more information.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…