PowerShell for N00bs 3: What’s a.k.a. short for?

"Also known as."  It's an abbreviation, a TLA.  (What's a TLA short for?  "Three Letter Acronymn."  See: recursion, definition.)

In PowerShell for N00bs 2, we snuck something in the last example:

PSH> (gm -InputObject [string] | where-Object {$_.name -eq 'replace'}).definition
System.String Replace(Char oldChar, Char newChar), System.String Replace(String oldValue, String newValue)

'gm' here stands for 'Get-Member.'  It's an alias.  Let's create one - as an old school Unix user, my fingers are used to typing 'less' when I want to paginate output.

PSH> New-Alias less more

Now, to test it,

PSH> dir $env:temp | less

Works like a charm.  But, it doesn't save us any keystrokes.  Now, let's set aside our Unix experience and try to be lazy. 

PS> New-Alias ls more
New-Alias : Alias not allowed because an alias with the name 'ls' already exists.
At line:1 char:10
+ New-Alias  <<<< ls more

Ouch.  Well, what does it do?

PSH> Get-Alias ls

CommandType  Name  Definition
-----------  ----  ----------
Alias        ls    Get-ChildItem

And what's 'Get-ChildItem?'  Let's run it and see.

Oh, it's 'dir'.  Wait a second...

PSH> Get-Alias dir

CommandType  Name  Definition
-----------  ----  ----------
Alias        dir   Get-ChildItem

'dir' is indeed an alias.  In fact, most of what we're used to doing in cmd.exe (and /usr/bin/bash, for that matter) has been aliased. 

PSH> Get-Alias

That's a lot of aliases.  Quick review: how many?  Get-Alias returns a list of items (an array in PSH-speak), and every array object has a property of the number of elements it contains.

PSH> (Get-Alias).count

That's nice, but they're all out of order.  Let's organize them by alias name so we can better see what we have.

PSH> Get-Alias | Sort-Object -Property Name 

Next: we know 'dir' is an alias for Get-ChildItem.  Any others?  Yes, we can sort the alias list by Definition, but we'll still have to scan for Get-ChildItem.

PSH> get-Alias | Where-Object {$_.Definition -match "Get-Childitem"}

CommandType  Name  Definition
-----------  ----  ----------
Alias        gci   Get-ChildItem
Alias        ls    Get-ChildItem
Alias        dir   Get-ChildItem

'gci' makes sense in a TLA-sort of way.  In fact, if we sort the alias list by Definition, we'll find most aliased commands have one alias that is some abbreviation or truncation of the command's name. 

To review, we learned three things:

  • PowerShell comes with a bunch of aliases for common commands.

  • We can dump the list to see what it provides, and sort it by either alias or definition.

  • We can create more, so long as we don't overwrite existing ones.  (More on that later.)

Comments (0)

Skip to main content