PowerShell Overview: Aliases and Functions

Does the New-Alias cmdlet only allow you to create aliases for cmdlets themselves? Can you create an alias that actually covers several commands?


While we were conducting our lab at TechReady 12 we conceded that Windows PowerShell can require a lot of typing. (A lot of typing.) One suggestion we had for reducing the amount of typing you have to do is to create aliases for some of your cmdlets. For example, suppose you don't like typing Get-CsVoicemailReroutingConfiguration. (We're not sure why you wouldn't want to type all that, but, hey, to each his own.) One way to work around all that typing is to create an alias for that cmdlet:


New-Alias –Name vrc –Value Get-CsVoicemailReroutingConfiguration


What's the point of that? Well, after creating the alias you'll no longer have to type Get-CsVoicemailReroutingConfiguration; instead, you can just type the alias (vrc) and PowerShell will go ahead and call Get-CsVoicemailReroutingConfiguration.


That's what we said: cool.


But while aliases are definitely nice, there are other ways to minimize the typing of cmdlet names. (Like, say, tab expansion.) Finding a shortcut way to type cmdlet names is great, but what people really want to do is create an alias for a much more complicated command, like this one:


Get-CsAdUser –LdapFilter "Department=IT" | Format-Table DisplayName, Enabled, SipAddress –AutoSize


So how do you create an alias for a command like that? Well, you don't: PowerShell will only let you create aliases for cmdlets, functions, scripts, or files. In the preceding example you can create an alias for the Get-CsAdUser cmdlet (without the –LdapFilter parameter) but that's about it.


Note. Well, OK, you could create an alias for the Format-Table cmdlet. But you don't really need to do that, because an alias already exists for that cmdlet:




So is there a way to work around this problem? Of course there is: there's always a way to work around a problem. (Well, except when there isn't.) For one thing, you could write a script that runs that command for you; there's definitely nothing wrong with that. However, an even better approach might be to create a function that runs your command for you. You know, a function that looks like this one:


Function itusers {Get-CsAdUser –LdapFilter "Department=IT" | Format-Table DisplayName, Enabled, SipAddress –AutoSize}


What we have here is a little function named itusers, a function that returns all the users in the IT department and then displays the value of the DisplayName, Enabled, and SipAddress attributes for each of those users (and in a nicely-formatted table to boot). Now that we have this function do we still have to type in this command?


Get-CsAdUser –LdapFilter "Department=IT" | Format-Table DisplayName, Enabled, SipAddress –AutoSize


Of course not. Instead, all we have to do is call the function:




See? It's just like an alias, only better: after all, we can use our function to call multiple commands.


Which, if we recall correctly, is just exactly what wanted to do in the first place.


Note. We should mention that functions (and aliases) tend to disappear whenever you exit and then restart Windows PowerShell. To make sure that your functions don't disappear when you exit and then restart Windows PowerShell, you might consider stashing these functions in your PowerShell profile.

Skip to main content