Home > other >  Powershell alias doesn't seem to work with alias name that contains -
Powershell alias doesn't seem to work with alias name that contains -

Time:04-30

When I type :

Set-Alias -Name test2 -Value test

Get-Alias shows test2 points to test

But with

Set-Alias -Name test2-clone -Value test

Get-Alias shows test2-clone but it doesn't point to test

CodePudding user response:

While the alias does not display properly, it does still get set correctly:

# Set two examples
Set-Alias TestAlias -Value Get-Date
Set-Alias Test-Hyphen -Value Get-Date

# Doesn't display correctly from Get-Alias
Get-Alias Test*

CommandType Name                   Version Source
----------- ----                   ------- ------
Alias       TestAlias -> Get-Date                ## Works
Alias       Test-Hyphen                          ## Doesn't show command?

# Check the alias properties to see the command definition is set correctly:
Get-Alias Test-Hyphen | Select DisplayName,Definition

DisplayName Definition
----------- ----------
Test-Hyphen Get-Date  

# Use the alias, and it works fine:
Test-Hyphen

Wednesday, April 27, 2022 10:00:08 AM

This is an (old) bug in the View for commandinfo type objects defined in C:\Windows\System32\WindowsPowerShell\v1.0\PowerShellCore.format.ps1xml

This is on purpose, per the Get-Alias help page:

The output shows the -> format that was introduced in Windows PowerShell 3.0. This format is used only for aliases that do not include hyphens, because aliases with hyphens are typically preferred names for cmdlets and functions, rather than nicknames.

  • Related