Home » PowerShell » Set Alias Permanent in PowerShell

Set Alias Permanent in PowerShell

Using Export-Alias and Import-Alias cmdlets in PowerShell, you can set alias permanent in PowerShell. Export-Alias command exports all alias information and using the Import-Alias cmdlet, alias information can be imported.

In PowerShell, you can use New-Alias and Set-Alias cmdlets to create or set an alias for the PowerShell commands.

However, created or set alias information is available for the current session or till you close PowerShell.

Hence, to save alias permanently in PowerShell, use Export-Alias and Import-Alias cmdlets.

In this article, we will discuss how to use Export-Alias and Import-Alias cmdlets in PowerShell to set permanent alias.

PowerShell Set Alias Permanent

Use the Export-Alias cmdlet in PowerShell to export all alias information in the file format like CSV or text file.

Export-Alias -Path D:\PS\Alias.txt

In the above PowerShell script, the Export-Alias command uses the Path parameter to specify the name of the file path that will contain the alias information.

If you want to check the content of the alias information, use the Get-Content command to read the content of the file as follows.

 Get-Content -Path D:\PS\Alias.txt 

The output of the above PowerShell script reads the content of the file and displays it on the terminal as below.

PS C:\> Get-Content -Path D:\PS\Alias.txt                                                                               

# Alias File
# Exported by : ShellGeek

# Date/Time : 30 July 2022 18:57:47
# Computer : INCORP-EU-117

"foreach","ForEach-Object","","ReadOnly, AllScope"
"%","ForEach-Object","","ReadOnly, AllScope"
"where","Where-Object","","ReadOnly, AllScope"
"?","Where-Object","","ReadOnly, AllScope"
"ac","Add-Content","","ReadOnly, AllScope"
"clc","Clear-Content","","ReadOnly, AllScope"
"cli","Clear-Item","","ReadOnly, AllScope"
"clp","Clear-ItemProperty","","ReadOnly, AllScope"
"clv","Clear-Variable","","ReadOnly, AllScope"
"compare","Compare-Object","","ReadOnly, AllScope"
"cpi","Copy-Item","","ReadOnly, AllScope"
"cpp","Copy-ItemProperty","","ReadOnly, AllScope"
"cvpa","Convert-Path","","ReadOnly, AllScope"
"dbp","Disable-PSBreakpoint","","ReadOnly, AllScope"
"diff","Compare-Object","","ReadOnly, AllScope"

To import the alias information contains in the file, use the Import-Alias command.

Import-Alias -Path D:\PS\Alias.txt -Force

In the above PowerShell script, the Import-Alias script takes the name of the file path as input to import the file and alias information.

Note: Use the Force parameter to overwrite the existing alias information. If you don’t use the Force parameter, it will throw an error like The alias is not allowed, because an alias with the name ” already exists.

Upon the successful import of the alias information, it will imports alias list and set permanent alias in PowerShell.

Cool Tip: How to remove an alias in PowerShell!

Conclusion

I hope the above article on how to set alias permanent in PowerShell is helpful to you.

Using the Export-Alias cmdlet, you can export the alias list in PowerShell and save it in file format.

Later using the Import-Alias cmdlet, you can import the alias list and overwrite the existing alias list. It will set permanent alias in PowerShell.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.