Get-AllAliases

Paul Masek
PowerShell Explained
2 min readJun 1, 2017

I personally find Get-Alias incredibly useful. During seasons as a sysadmin when I’m using PowerShell frequently I refer to Get-Alias both for figuring out what the full command is, as well as looking up what aliases a command has quite often. Many times though, what I’ll do is first plug in the alias name to discover the full command name (e.g. Get-Alias ls) and I’ll follow that up with discovering what other aliases that command may have are (e.g. Get-Alias -Definition Get-ChildItem). This struck me as repetitive and unnecessary. Why do two commands, when I can accomplish this same process with one?

Enter Get-AllAliases.

Get-Alias ls |ForEach-Object {Get-Alias -Definition $_.Definition}

This is a nice one-liner that is even more at home in a function placed inside a PowerShell profile like so:

function Get-AllAliases ($Name){
Get-Alias $name |
ForEach-Object {
Get-Alias -Definition $_.Definition
}
}

Here’s how I went about creating this one-liner / function.

First of all, I knew that I needed the definition (full command name) of the alias, so I looked to see which property to utilize after the initial Get-Alias command is run. The following command allows me to see the full output of Get-Alias: Get-Alias | Format-List *. The output of that command looked like this:

HelpUri             : http://go.microsoft.com/fwlink/?LinkID=113308
ResolvedCommandName : Get-ChildItem
DisplayName : gci -> Get-ChildItem
ReferencedCommand : Get-ChildItem
ResolvedCommand : Get-ChildItem
Definition : Get-ChildItem
Options : ReadOnly, AllScope
Description :
OutputType : {System.IO.FileInfo, System.IO.DirectoryInfo}
Name : gci
CommandType : Alias
Source :
Version :
Visibility : Public
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters : {[Path, System.Management.Automation.ParameterMetadata], [LiteralPath,
System.Management.Automation.ParameterMetadata], [Filter, System.Management.Automation.ParameterMetadata],
[Include, System.Management.Automation.ParameterMetadata]...}
ParameterSets :

This gave me not just one, but several properties to choose from. The “Definition” property happened to be the shortest one.

So I take the “Definition” property output from Get-Alias and pipe it to ForEach-Object. ForEach-Object allows me to perform an operation on the pipeline object that I’ve chosen. Specifically the operation that I choose to perform is Get-Alias -Definition. The syntax that I use to pass on the “Definition” property is $_.Definition, $_ is the current pipeline object and .Definition specifically chooses the “Definition” property of the current pipeline object.

The function adaptation of this one-liner includes a named parameter $Name that, when ran, receives the input from the command line as a variable in the first portion of the script Get-Alias $Name.

I’ve managed to get each of these posts shorter than the last. Yes, they’re all a bit long-winded, but that’s the point of this blog. Hopefully reading this post was informative and you're moving forward with PowerShell knowledge that you didn’t have prior. Please feel free to leave any comments or critiques that you may have. I’m trying to learn PowerShell and am definitely not an expert, just a student trying to pass on knowledge to other students!

Microsoft Technet Article on PowerShell Profiles : https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx

--

--

Paul Masek
PowerShell Explained

IT Polyglot (Windows Systems Engineer / Windows SysAdmin / Linux SysAdmin / PowerShell Enthusiast) who dislikes repetitive tasks and loves automation.