Friday Fun: I Can Run that Command in 3 Letters

Large Blog ImageIf you have been using PowerShell for any length of time, I’m sure you are familiar with aliases. An alias is an alternative name to a PowerShell cmdlet. They are intended to serve as transition aids (like dir and ls) and as a means to keep interactive typing to a minimum. Although with tab completion, if you can get in the habit, perhaps this isn’t as big a deal. However, there are a number of aliases you might want consider creating if, like me, you spend a lot of time at a PowerShell prompt.

I still use Notepad, often as a temporary buffer. I’m too lazy to type “notepad” at a prompt. So I have this:

I use Set-Alias instead of New-Alias so that if I am trying to define something that already exists I won’t get any errors. You can also make your aliases Read-Only so you don’t accidently change them.

Another one that I’ve started using is an alternative to Select for Select-Object.

The Name and Value parameters are positional. Like you, I use a number of Office products.

Here’s one that might escape my younger readers.

I have a separate script file that defines all of my aliases which is dot-sourced in my profile script. I use a few Sysinternals utilities and while I could modify %PATH% it is just as easy to create a few aliases.

My other shortcuts are helper (cheater?) functions like these where the name is more like an alias.

As you can see, I’m using PowerShell aliases for more than cmdlets. What kind of shortcuts and aliases have you come up with?

More PowerShell Laziness

lightbulb-ideaA few days ago I posted an article on using Update-TypeData to provide shortcuts to object properties. These shortcuts might save a few keystrokes typing, especially if you use tab completion. They can also give you more meaningful output. But you can take this even further and save yourself even more typing. How many of you have struggled to type an expression like this:

If you read the last article, you already know that I can use my alias property shortcuts. But you can also define other types of properties. Let’s say I often want to get file sizes in KB, and it is pretty tedious having to use Select-Object all the time with a custom hashtable. Instead I can do this with Update-TypeData:

When defining a ScriptProperty, the $this variable is used instead of $_. Using my previously created aliases, my command is now much easier to write:

With the aliases I can use them anywhere in a PowerShell expression. Let me leave you with one more example:

I often need to get the total value of something, but often I need it in a different format such as MB or GB. Now I have it.

That definitely saves some typing. I should probably do something similar for Maximum and Minimum properties as well.

So, what are you constantly typing? Is it something you could be smarter about? I hope you’ll share so I can take advantage of your laziness, I mean, efficiency!