Warm tip: This article is reproduced from serverfault.com, please click

PowerShell 7.1, How do I Get Aliases from a List of Functions Names

发布于 2020-11-29 07:18:27

I'm fairly new to Powershell so I may be missing something, so hear goes:

Note: I prefix all my functions with my initials Function mts_do_something()

I'm trying to walk through a list of Functions ( Get-Command mts_* ) and get their associated aliases.

However, some of the Functions don't have aliases ( so Get-Alias -Definition mts_do_something ), this command returns an error. So I walk through the list of aliases and try to match them to the item from the 'GCM'.

Sample Code:

Get-Command mts_* | foreach-object -Process { 
    $name = $_.ToString() 
    $aliasName = ""
    Get-Alias -Definition mts_* | ForEach-Object -Process { 
        if($_.Definition -eq $Name) { 
            $aliasName = $_.Name 
        }
    }
    if([string]::IsNullOrEmpty($aliasName.ToString().Trim()))
    {
        Write-Host "  " $name.PadRight(30)
    }
    else
    {
        Write-Host "  " $name.PadRight(30) " --> " $aliasName
    }
}

Sample Output:

mts_do_something 
mts_my_function       -->   myf
mts_fix_car
mts_walk_dog          -->   wDog

Question: While this code produces the desired output, is there an elegant way to do this?

Questioner
Mark T. Schwartz
Viewed
0
mklement0 2020-11-30 22:03:17

this command returns an error.

You can simply ignore these errors, using the common -ErrorAction parameter, which simplifies your approach:

Get-Command mts_* | ForEach-Object { Get-Alias -ErrorAction Ignore -Definition $_ }

Caveat: This won't find aliases of commands from auto-loading modules that haven't yet been loaded (imported) into the current session; note that such modules are implicitly loaded the first time you call any of their non-alias commands.
Similarly, Get-Alias only reports aliases from currently loaded (imported) modules, so the caveat also applies to Scepticalist's answer, which additionally has the disadvantage of having to call Get-Alias to retrieve and filter all aliases for every input command name.

Note that Get-Alias's output formatting includes both the alias name and the definition (target command), so that you'll see something like:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           foo -> mts_foo
Alias           bar -> mts_bar

If instead you want a two-column display limited to the alias name and its definition, you can use Select-Object:

Get-Command mts_* |
  ForEach-Object { Get-Alias -ErrorAction Ignore -Definition $_ } |
    Select-Object Name, Definition

The advantage of using Select-Object is that it outputs objects suitable for later programmatic processing.

You'll get something like:


Name Definition
---- ----------
foo  mts_foo
bar  mts_date

However, in order to control the display output you can instead use Format-Table, which gives you more control over the formatting - but prevents subsequent processing of the output, given that Format-* cmdlets output formatting instructions rather than the original data.

For instance, the following gets closer to your original output format:

Get-Command mts_* |
  ForEach-Object { Get-Alias -ErrorAction Ignore -Definition $_ } |
    Format-Table  Name, { '->' },  Definition -HideTableHeaders

The { '->' } script block is a (simple) example of a calculated property, which are typically hashtable-based (@{ ... }), and in the case of Format-Table even allow you to control the column width via a width entry.

You'll get something like:


foo  ->     mts_foo
bar  ->     mts_bar