While the examples show commands that search file content based on Get-ChildItem input - without clearly saying so, incidentally - it would be helpful to add the following clarifications:
-
While input objects that aren't already strings are stringified with .ToString() in order to be searched, System.IO.FileInfo input - typically from Get-ChildItem / Get-Item calls - is special-cased:
Select-String searches the content of the files these FileInfo instances represent.
-
The "Inputs" section should also mention System.IO.FileInfo as special-cased.
On a more general note:
-
The .ToString() stringification in many cases results in unhelpful results, because it isn't the rich string representation produced by PowerShell's formatting system that one would intuitively expect to be used.
-
The upshot in terms of practical advice is:
- Pipe any non-string and non-file input via an intermediate
oss call in order to search the for-display representation of the input (what you would see in the console); e.g.:
# !! NO output, due to .ToString() stringification
PS> @{ foo = 'bar'; baz = 'quux' } | Select-String quux
# OK, thanks to `oss`
PS> @{ foo = 'bar'; baz = 'quux' } | oss | Select-String quux
baz quux
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
While the examples show commands that search file content based on
Get-ChildIteminput - without clearly saying so, incidentally - it would be helpful to add the following clarifications:While input objects that aren't already strings are stringified with
.ToString()in order to be searched,System.IO.FileInfoinput - typically fromGet-ChildItem/Get-Itemcalls - is special-cased:Select-Stringsearches the content of the files theseFileInfoinstances represent.The "Inputs" section should also mention
System.IO.FileInfoas special-cased.On a more general note:
The
.ToString()stringification in many cases results in unhelpful results, because it isn't the rich string representation produced by PowerShell's formatting system that one would intuitively expect to be used.For that,
Out-String -Streamwould have to be used for stringification, and PowerShell ships with convenience functionossthat does just that.As an aside: Of course, it would make sense for this to be
Select-String's default behavior - see Make Select-String more intelligent for non-primitive types PowerShell/PowerShell#10726The upshot in terms of practical advice is:
osscall in order to search the for-display representation of the input (what you would see in the console); e.g.:Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.