I get all the packages installed on my PC using Get-AppxPackage
and I\'m trying to find all the matches in that with N lines before and after using Select
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, say via a switch parameter named -FromFormattedOutput
, as detailed in this feature request 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.