powershell

How to create permanent PowerShell Aliases


I want to create an alias of a cmdlet that doesn't expire after I close the current session of Powershell, let's say I have this alias :

C:\Users\Aymen> New-Alias Goto Set-Location

This perfectly creates the Goto alias, but I want to use it even after I close the current session, how can I achieve that.

Note:

The PowerShell Help system suggests that I can export the aliases I create, and import them next time I open a new session, actually that's not really what I'm looking, for, is there a direct clear way to keep having a alias after I create it through different sessions


Solution

  • UPDATED - January 2021

    It's possible to store in a profile.ps1 file any PowerShell code to be executed each time PowerShell starts. There are at least 6 different paths where to store the code depending on which user has to execute it. We will consider only 2 of them: the "all users" and the "only your user" paths (follow the previous link for further options).

    To answer your question, you only have to create a profile.ps1 file containing the code you want to be executed, that is:

    New-Alias Goto Set-Location
    

    and save it in the proper path:

    IMPORTANT: remember you need to restart your PowerShell instances to apply the changes.

    TIPS