This topic contains 4 replies, has 3 voices, and was last updated by
June 8, 2014 at 5:37 pm #16034ParticipantPoints: 0Rank: Member
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
June 8, 2014 at 5:44 pm #16035KeymasterPoints: 1Rank: Member
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.
June 8, 2014 at 9:11 pm #16038KeymasterPoints: 1Rank: Member
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.
June 8, 2014 at 8:59 pm #16037ParticipantPoints: 0Rank: Member
I still don't understand however, why where-object doesn't read the entire contents of the "name" object.
dir -> Get-ChildItemis the output of name, right? so why does where-object work with get-children, but not with dir.
June 9, 2014 at 6:20 pm #16070ParticipantPoints: 0Rank: Member
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
The topic ‘two issues I noticed in get-alias’ is closed to new replies.