Question:
I am newbie on powershell. Today I tried something very simple
1 2 |
Alias | sls -Pattern 'echo' |
which produced echo
, but what I want is Alias echo -> Write-Out
.
In bash, you can just do
1 2 |
alias | grep 'echo' |
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:
1 2 |
alias -name echo |
The echo -> Write-Out
is the DisplayName
:
1 2 |
(alias -name echo).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:
1 2 |
Alias echo -> Write-Output |
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:
1 2 |
alias | %{$_.tostring()} |
Therefore any matches from Select-String
only match on the alias name.