Home > front end >  PowerShell: Short way to find String in Object-List with "Select-String"
PowerShell: Short way to find String in Object-List with "Select-String"

Time:12-16

Problem

I'm trying to find a way to find a string in a object list, like the result of Get-Alias. My problem is that all solutions are either way too long to be practical to use or do not result in the behavior I need.

What I've tried so far:

Using alias | sls -All "Get-". No result, because sls uses toString which is only the name column:

enter image description here


  1. Using alias | Out-String | sls -All "Get-". Only highlights

enter image description here


  1. Using alias | Where-Object {$_.Definition -like "*Get-Alias*"}. A lot to write and requires knowing the column where the text is.

enter image description here


  1. Using alias | findstr "Get-". Works, but requires the use of a legacy executable that is not available on all PowerShell Core supported platforms. I want the code to work on all platforms without switches.

enter image description here


  1. Write to a file and then pipe that to Select-String. Very inpractical.

...


Can someone help me with this problem?

CodePudding user response:

You want to grep the objects as you see them displayed on your console, your first example using Out-String was going in the right direction except you were missing the -Stream switch:

alias | Out-String -Stream | sls -All "Get-"

Or if you want to have it shorter, you can use the built-in wrapper oss:

alias | oss | sls -All "Get-"
  • Related