1

I'm reading "PowerShell Core for System Administrators" and I came to this example:

get-help get-command | out-string -s | select-string 'wildcard'

Seems equivalent to:

program-that-outputs-lots-of-text | grep 'wildcard'

But I don't understand the middle part. The explanation given is that output-string -s chops up the input into individual strings rather than one big one. In that case I would expect omitting the middle part would lead to outputting the entire help file (because it's one string and it matches), but instead nothing at all comes out. Why is that the case?

zett42
  • 25,437
  • 3
  • 35
  • 72
readyready15728
  • 518
  • 3
  • 17
  • 2
    does this answer your question? https://stackoverflow.com/a/65736460/7411885 – Cpt.Whale Apr 08 '21 at 19:15
  • 3
    `Get-Help` doesn't output a `String` but an object (try `(Get-Help Get-Command).GetType()`). It has to run through the PowerShell formatting system to produce the output you see in the console. Otherwise cmdlets such as `Select-String` use simple string _conversion_ such as provided by .NET method `ToString()`, often not useful at all. `Out-String` makes sure the PowerShell formatting system is actually used when output to a string. – zett42 Apr 08 '21 at 19:37
  • readyready15728: I hope that @zett42's comment here, in combination with the linked duplicate, answers all your questions. Ideally, there'd be no need for the intermediate `Out-String -Stream` call (recent PowerShell versions have a built-in wrapper function succinctly named `oss`), which is the subject of [GitHub issue #10726](https://github.com/PowerShell/PowerShell/issues/10726#issuecomment-567628146) – mklement0 Apr 08 '21 at 21:32
  • also, unlike `grep`, `Select-String` does a case-insensitive search by default – phuclv Apr 09 '21 at 00:56
  • @Cpt.Whale Not quite but the other comment did – readyready15728 Apr 09 '21 at 03:19

0 Answers0