Powershell on the Way to Work–Part 4


Traffic was rotten today so I’m sitting in a coffee shop.  Don’t tell the boss ok?

So last time I mumbled something or other about Aliases and did a Wave of my hands to make you look the other way.  No ? I didn’t do that?  Darn Jedi mind tricks.

So we mentioned Aliases.    Those familiar with the Unix world will know what an Alias is.   It’s just another word or set of characters to represent something else.  Sometimes to ease typing, sometimes to give us a comfort zone.

We showed you GET-CONTENT also worked if you did TYPE.  Again this was because an ALIAS already existed for this.

If you’re curious… we’ll take a few minutes to look at Aliases.  They’re kinda neat.

If you type in

GET-ALIAS

This will show you a list of the current Aliases active in your Powershell console.   But since very few people can read as fast as I can normally talk, I would recommend our friend MORE get’s tacked on.

GET-ALIAS | MORE

image

We can see Aliases for most of the Cmdlets.   If you look near the bottom you’ll see a familiar one called ‘dir’ – When you type in ‘dir’ in Windows Powershell it’s ACTUALLY running ‘GET-CHILDITEM’.  Type it in and take a look at the results

DIR

You’ll see output that looks an AWFUL lot like the DIR command in CMD.EXE and DOS.  That’s because GET-CHILDITEM run against a folder will do exactly that.   So although GET-CHILDITEM is an unfamiliar command to you since you’ve just started in Windows Powershell, it DOES work very much (although not the SAME) as DIR.   Using the Aliases can help Powershell become more comfortable using familiar names.

Now if you’re curious what is the Powershell Cmdlet for a particular Alias, just ask Powershell.  So let’s say we were using what we THOUGHT was the TYPE command and we needed to know what it really was in Powershell?  Just key in.

GET-ALIAS type

image

Afterward you’ll see output on your screen showing you the Alias name “type” under the heading “Name” and the actual Powershell Cmdlet “Get-Content” under “Definition

Now the flip side is what if you found a Powershell Cmdlet and were curious if there are any predefined Aliases for it?  That’s an easy one as well

GET-ALIAS –Definition Get-Content

Again how I figured most of that out was play with GET-HELP GET-Content –examples to see if there was an example of how to use this particular Cmdlet.  I found the third example provided in help had what I wanted but I had to edit the name of the Cmdlet.

If you’re feeling curious I can take a few moments and show you how to make an Alias.  It’s actually very easy.  We use the “NEW-ALIAS” Cmdlet.

So let’s say we’d like to make an extra alias for Get-Content called “showfile

All we’d have to do is type in

NEW-ALIAS –name showfile –value Get-Content

Now you can type in showfile as well as type for Get-Content.   Take note, this is a temporary change and will go away. 

Yes you can make it permanent but my Boss is now buzzing me.  I should actually get back on the road to work

Until next time, Keep on Shellin’

DSC_0158 (640x425)_thumb

Sean Kearney
Twitter: @energizedtech
www.powershell.ca

Comments (0)

Skip to main content