Strange behavior with the Get-Alias cmdlet

This topic contains 7 replies, has 3 voices, and was last updated by  Mike F Robbins 3 years, 3 months ago.

  • Author
    Posts
  • #16229

    Mike F Robbins
    Participant

    Does anyone know why the following command treats the dollar sign as a wildcard instead of a literal:

    Get-Alias -Name ?
    

    Is there a way to use an escape character to treat the dollar sign as a literal in the previous command? It causes all aliases that have a name with a single character to be returned instead of only the alias "?" (Where-Object).

    Expected behavior would be the same as the following:

    Get-Alias | Where-Object Name -eq ?
    

    Just something I ran into and wanted to see what others thought about this.

  • #16230

    Don Jones
    Keymaster

    Escape it using a back tick. The cmdlet doesn't have a -LiteralName parameter.

  • #16231

    Mike F Robbins
    Participant

    Same results using the back tick to escape it:

    Get-Alias -Name `?
    

    Looks like internally, the Name parameter of Get-Alias must do a "like" match on the value entered as the following returns the same results:

    Get-Alias | Where-Object Name -like ?
    
  • #16232

    Don Jones
    Keymaster

    Sweet. The new code formatter works awesomely.

  • #16233

    Mike F Robbins
    Participant

    Even the back ticks work in the new code formatter 🙂

    Clicking the "Preformatted" button once is all that's needed which results in "pre]{content}[/pre" being added. I replaced the content including the curly braces with my code. A common mistake might be trying to click the button before and after your code like the old code formatter.

  • #16234

    Mike F Robbins
    Participant

    Based on responses from @SystematicADM that I received on Twitter, both of the following work so these are ways to escape the wildcard searching and search for the literal question mark without having to resort to piping to Where-Object.

    Get-Alias -Name ``?
    Get-Alias -Name '`?'
  • #16244

    Dave Wyatt
    Moderator

    When you need to escape one of the special characters in the usual "like" wildcards, you can place them in square brackets. (I didn't know about embedding a backtick in the string as well, which seems to work). You could also use Get-ChildItem on the alias:\ drive, which does have a -LiteralPath parameter:

    Get-Alias [?]
    Get-ChildItem -LiteralPath Alias:\?
    
  • #16246

    Mike F Robbins
    Participant

    Thanks Dave! That gives me a number of options depending on the particular scenario.

You must be logged in to reply to this topic.