Marius Gheorghe

Building software : make it work, make it good, make it fast

Powershell aliases are worthless

Powershell aliases are mostly a waste of time. They have 2 big problems :
- you can't alias a command with multiple parameters. Basically you can't have

Set-Alias scxc "svn update c:\work\trunk"

Actually you can set that alias. But it fails when you run it. So as a workaround you'll have to use a function

function scxc()
   svn update c:\work\trunk

Which brings me to the second problem.
- aliases aren't persisted by default. You have to use Export-Alias and Import-Alias between sessions. And here's the kicker : aliases with functions can't be exported and imported correctly.

A much better solution is to skip aliases and add all this stuff directly to your powershell profile (by default you can find it in %userprofile%/MyDocuments/WindowsPowerShell/Microsoft.PowerShell_profile.ps1).
Just edit it and add functions there that act as aliases.

Comments (4) -

  • Jeffery Hicks

    12/4/2012 4:16:31 PM |

    You are misunderstanding the purpose of an alias. I also think you are conflating the purposes of aliases and functions.

    An alias is intended as a shortcut name to a longer cmdlet that makes it easier to use at a prompt. Thus, instead of having to type Import-Module, you can type ipmo. The other purpose for aliases is to serve as a transition for people coming from other shells. So Linux people can use ls and CMD.EXE people can use dir. Now, they are not running ls or dir as they know it, but it eases the transition to PowerShell.

    You are correct in that you need to use functions for your common commands that need parameters.

    An alternative might be to use a script block:
    PS C:\> $netstat = {netstat /n}

    Then use the Call operator whenever you want to run it.

    PS C:\> &$netstat

    Finally, the persistence point is part of PowerShell's scope. The answer, as you point out, it to put your aliases and helper functions in your profile script. That is exactly what I do.

    • Marius Gheorghe

      12/4/2012 4:55:11 PM |

        Yes, i should have been more clear that i meant this in the specific case where you have multiple parameters. Aliases are indeed useful in the "no parameters" scenario. The blog post  was poorly chosen.

      Script blocks can do the same thing but there's still the persistence problem.

  • Jeffery Hicks

    12/4/2012 5:00:44 PM |

    I don't see lack of persistence a problem, it is a manifestation of scope which all programming languages have.  Variables, PSDrives, new functions also don't persist between sessions, nor would you want them to. That is the purpose of the  profile script: to configure your session with the variables, aliases, functions and drives that you need.

    By the way, I have never liked, or really used the  export/import alias cmdlets. It might make sense in a module where you want to import a list. But I simply define them in my profile.

    • Marius Gheorghe

      12/4/2012 5:45:33 PM |

        If the scope of the discussion if limited to the programming language you are right. However if we talk about powershell as a actual shell replacement i think the persistance thing IS a problem. Well, it's fixable by editing the profile but from a "dogmatic" point of view it's a problem.

Comments are closed