PowerShell - How do I define multiple commands in alias?

Solution 1:

For example:

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.

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).

Instructions as seen in here.