I found two issues with get-alias. Maybe I’m doing something wrong. First one is that the alias of cmdlets are not found.
If I type `get-alias ls`, the appropriate cmdlet is printed out. If I type `get-alias get-children`, I get zippo.
I tried filtering the results of get-alias with where-object, but I weirdly doesn’t work.
`gal | out-string |? Name -like "Set-Alias"`
outputs nothing, but
`gal | out-string |? Name -like "sal"`
works and print out
Alias sal -> Set-Alias
You’re doing something wrong :).
First, LS is an alias. Get-Children isn’t an alias. “Get-ChildItem” is a command name; it won’t show up from Get-Alias because it isn’t an alias. It’s the real command name.
Out-String fundamentally breaks everything else in the pipeline for you’re doing. You’re taking the output of Get-Alias, which is completely filterable and sortable, and turning it into a big chunk of text. Out-String doesn’t belong in there. Once you convert something to a string, you shouldn’t expect to sort it, select it, filter it, or do much else with it. PowerShell isn’t designed to work with strings, it’s designed to work with objects.
Similarly, “Set-Alias” isn’t an alias. I wouldn’t have expected that to work anyway.
Get-Alias | Where ResolvedCommandName -eq "Get-ChildItem"
Will return the aliases gci, ls, and dir.
Will return the alias ls.
I still don’t understand however, why where-object doesn’t read the entire contents of the “name” object.
dir -> Get-ChildItem is the output of name, right? so why does where-object work with get-children, but not with dir.
You’re not following the logic.
First, it’s Get-ChildItem, not “get-children.” That’s important.
Returns output because there is an alias, Dir. That output is an object, and it has a Name property.
Get-Alias dir | where name -eq 'dir'
Works because Where-Object is getting input.
Returns NO OUTPUT because there is no alias named Get-ChildItem. Get-ChildItem is a command, not an alias. Therefore,
Get-Alias Get-ChildItem | Where Name -eq Get-ChildItem
Also returns no output, because Where-Object had no input.
Get-Alias dir | Out-String | Where name -eq 'dir'
Returns no output, because the input to Out-String was converted from an object to a simple string of text. Strings of text don’t have “name” properties. Out-String is what’s breaking that example. Similarly,
dir -> Get-ChildItem
Is not “the output of name.” Run this:
Get-Alias -name dir | Select -Property *
And you will see what the “name” property contains. That example will show you all of the properties of the object output by Get-Alias.
I think you might not be entirely understanding some of the fundamental bits about how the shell works, which is what makes it more confusing perhaps. The Format and Out commands in particular can be confusing because it is not always obvious that they are doing something different. It might be worth your time to watch some of the videos on youtube.com/powershelldon, to get a better idea of those fundamentals.
Your first example uses the “Name” positional parameter:
Which is the same as:
Get-Alias -Name ls
With the second example you provided, you need to use a different parameter set. You want to use the “Definition” parameter and it’s not positional so it can’t be omitted:
Get-Alias -Definition Get-ChildItem
Same thing with the third example:
gal -Definition Set-Alias
Here’s all you need for the forth example:
Take a look at the full help for Get-Alias and I think all of this will become self-evident.
help Get-Alias -Full