Strange behavior with the Get-Alias cmdlet

Welcome Forums General PowerShell Q&A Strange behavior with the Get-Alias cmdlet

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
4 years, 4 months ago.

  • Author
    Posts
  • #16229

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1
    Rank: Member

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

  • #16231

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1
    Rank: Member

    Sweet. The new code formatter works awesomely.

  • #16233

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

The topic ‘Strange behavior with the Get-Alias cmdlet’ is closed to new replies.