How to create PowerShell alias permanently?

PowerShellMicrosoft TechnologiesSoftware & Coding

PowerShell alias can be created permanently by 2 methods below.

a) Import / Export Alias

To export all the aliases, you need to use Export-Alias cmdlet. When you use this command it will ask you the path for the file to import.

To export the newly created alias, you need to give alias name and the name for the export, so later you can import it with the same name.

In the below example, we have created alias name Edit for the Wordpad and we will export all the aliases with the name Alias1, so the newly created alias will also be stored and when you want to import your newly created aliases you need to write Import-Alias command.

Now, we will export all the aliases.

Export-Alias -Path D:\Temp\Alias1

You can check the exported aliases and manipulate them as well with the proper format.

Notepad D:\Temp\Alias1

Next, whenever you run the new PowerShell console, you won’t find the new aliases so you need to import the exported aliases.

Import-Alias -Path D:\Temp\Alias1

But when you run the above command, you will get an error that inbuilt aliases are already existed but we have remediation for it, we can use –Force parameter to forcefully overwrite those aliases.

Import-Alias -Path D:\Temp\Alias1 -Force

Now you see the newly created aliases as well in the PowerShell console.

b) Startup Profile Script.

Another option but easier than Import/Export option is to create a profile script so every time PowerShell opens it loads a startup profile and all the commands and scripts are loaded which resides inside that profile folder.

Here, we will create a profile file Profile.ps1 using below command on the $PROFILE path of your PowerShell.

notepad $((Split-Path $profile -Parent) + "\profile.ps1")

The above command will prompt to the user to create Profile1.ps1 on the $Profile path if it doesn’t exist and if it is already created then it will open the file to allow the user to manipulate.

Once a file is opened, edit the file to set your aliases. Here we will set two aliases in the file. Type the following two commands in notepad and save it.

Set-Alias edit notepad.exe
Set-Alias edit1 "C:\Program Files\Windows NT\Accessories\wordpad.exe"

Launch the PowerShell console again and when you type edit, it will open Notepad and when you type edit1, it will open Wordpad.

With this simple method, you can add as many aliases in the profile script and launch them through the PowerShell console.

raja
Published on 14-Feb-2020 11:34:44
Advertisements