why sls aka select-string does not work

Question:

I am newbie on powershell. Today I tried something very simple

which produced echo, but what I want is Alias echo -> Write-Out.

In bash, you can just do

My question is why sls does not work. BTW, if I replace sls with findstr, it worked.

Answer:

If you want to get an alias with a particular name you can do:

The echo -> Write-Out is the DisplayName:

The Get-Alias command returns a sequence of objects, each of which represents a single command alias. When displayed in powershell, these objects are formatted as a table containing the CommandType, Name and ModuleName properties.

When you pipe into findstr, it is the string representations of these columns which are being filtered, so any match displays the whole table row:

When you pipe into Select-String each object is being bound to the -InputObject parameter of the Select-String cmdlet. Since Select-String operates on text, it just calls ToString on the received object to get its string representation.

ToString only returns the Name property. You can see this by executing the following:

Therefore any matches from Select-String only match on the alias name.

Source:

why sls aka select-string does not work by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply