Skip to content

Clarify that FileInfo input objects cause the file's *content* to be searched, clarify input types that are meaningfully supported #8278

@mklement0

Description

@mklement0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-utilityArea - Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions