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?