two issues I noticed in get-alias

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Mike F Robbins Mike F Robbins 3 years ago.

  • Author
    Posts
  • #16034
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    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

  • #16035
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    Get-Alias ls

    Will return the alias ls.

  • #16038
    Profile photo of Don Jones
    Don Jones
    Keymaster

    You're not following the logic.

    First, it's Get-ChildItem, not "get-children." That's important.


    Get-Alias dir

    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.


    Get-Alias Get-ChildItem

    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,

    And no,


    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.

  • #16037
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    ok, thanks.

    I still don't understand however, why where-object doesn't read the entire contents of the "name" object.

    for example dir -> Get-ChildItem is the output of name, right? so why does where-object work with get-children, but not with dir.

  • #16070
    Profile photo of Mike F Robbins
    Mike F Robbins
    Participant

    Your first example uses the "Name" positional parameter:

    
    get-alias ls
    

    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:

    
    gal sal
    

    Take a look at the full help for Get-Alias and I think all of this will become self-evident.

    
    help Get-Alias -Full
    

You must be logged in to reply to this topic.