Share via


Using the Export-Alias Cmdlet

Saving Windows PowerShell Aliases

The Export-Alias cmdlet provides a way to export your Windows PowerShell aliases to a text file. In its simplest form you can just call Export-Alias along with the path to the text file. For example, this command exports your alias file as a comma-separated values list, saving the file as C:\Scripts\Test.txt:

Export-Alias c:\scripts\test.txt

The resulting text file will look similar to this:

# Alias File
# Exported by : kenmyer
# Date/Time : Saturday, May 06, 2006 4:12:53 PM
# Machine : TVSFRANK
"ac","Add-Content","","ReadOnly, AllScope"
"clc","Clear-Content","","ReadOnly, AllScope"
"cli","clear-item","","ReadOnly, AllScope"

Alternatively, you can export your aliases as a script file, a file that uses a series of Set-Alias cmdlets (and thus a file that can be run as a Windows PowerShell script in order to configure those aliases). To do that, tack on the -as parameter, setting the value to “script”. That command looks like this:

Export-Alias c:\scripts\test.ps1 -as "script"

And the resulting file looks like this:

# Alias File
# Exported by : kenmyer
# Date/Time : Saturday, May 06, 2006 4:14:04 PM
# Machine : TVSFRANK
set-alias -Name:"ac" -Value:"Add-Content" -Description:"" -Option:"ReadOnly, AllScope"
set-alias -Name:"clc" -Value:"Clear-Content" -Description:"" -Option:"ReadOnly, AllScope"
set-alias -Name:"cli" -Value:"clear-item" -Description:"" -Option:"ReadOnly, AllScope"

Just for the heck of it, you might also want to export a single alias or set of aliases. For example, suppose you’ve created a set of aliases, each one starting with the letter X. To export these aliases as a script (thus making it easy for someone else to configure those same aliases on their computer) use the -name parameter along with the wildcard X*:

Export-Alias c:\scripts\test.ps1 -name x*  -as "script"

By default Export-Alias will overwrite any existing files with the same name; if a file named Test.txt already exists then Export-Alias will overwrite that file. To prevent that from happening, just add the -noclobber parameter:

Export-Alias c:\scripts\test.txt -noclobber

If you run this command and Test.xml already exists you’ll get back the following error message:

Export-Clixml : File C:\scripts\test.txt already exists and NoClobber was specified.
Export-Alias Aliases
  • epal