ZetCode

PowerShell Import-Alias

last modified February 15, 2025

In this article, we will cover the Import-Alias cmdlet in PowerShell. This cmdlet imports aliases from a file into the current session.

Alias basics

An alias is a short name for a cmdlet or command in PowerShell. Aliases make commands easier to remember and type. PowerShell includes many built-in aliases. The Import-Alias cmdlet helps manage custom aliases across sessions.

Basic Import-Alias usage

The simplest way to use Import-Alias is with a file path. The file should contain alias definitions in a specific format. Each line defines one alias. The cmdlet reads the file and creates the aliases in the current session.

alias1.ps1
Import-Alias -Path "C:\aliases.txt"

This command imports aliases from the specified text file. The file should contain properly formatted alias definitions. Each alias becomes available immediately after import.

Importing aliases with -Scope

You can specify the scope for imported aliases using the -Scope parameter. Options include Global, Local, or Script scope. This controls where the aliases are available. Global scope makes them available throughout the session.

alias2.ps1
Import-Alias -Path "C:\aliases.txt" -Scope Global

This imports aliases with global scope, making them available everywhere. Without specifying scope, aliases are imported to the current scope only. Global scope is useful for persistent aliases.

Using -Force to overwrite existing aliases

The -Force parameter allows overwriting existing aliases during import. Without it, the cmdlet skips aliases that already exist. This is useful when you need to update alias definitions. Use with caution to avoid unexpected changes.

alias3.ps1
Import-Alias -Path "C:\aliases.txt" -Force

This command imports all aliases from the file, overwriting any existing ones. The operation proceeds without prompts or warnings when using -Force. Verify your alias file contents before using this parameter.

Exporting and then importing aliases

A common workflow is to export aliases first, then import them later. This preserves custom aliases between sessions. Use Export-Alias to create the file. Then use Import-Alias to restore them.

alias4.ps1
Export-Alias -Path "C:\myaliases.txt" -Force
Import-Alias -Path "C:\myaliases.txt"

This first exports all current aliases to a file, then imports them back. The -Force parameter ensures the export file is overwritten if it exists. This is useful for alias backup and migration.

Importing specific aliases from a file

You can filter which aliases to import using the -Name parameter. This accepts wildcards to match multiple aliases. Only matching aliases from the file will be imported. This provides selective alias management.

alias5.ps1
Import-Alias -Path "C:\aliases.txt" -Name "g*"

This imports only aliases starting with "g" from the file. The wildcard (*) matches any characters after "g". This helps avoid importing unnecessary aliases when you only need specific ones.

Verifying imported aliases

After importing, you can verify the aliases using Get-Alias. This lists all available aliases. You can filter the output to check specific imported aliases. This confirms the import operation succeeded.

alias6.ps1
Import-Alias -Path "C:\aliases.txt"
Get-Alias | Where-Object { $_.Source -eq "C:\aliases.txt" }

This imports aliases then lists only those imported from the specified file. The Where-Object cmdlet filters the results. This helps verify which aliases came from your import file.

Importing aliases with -PassThru

The -PassThru parameter returns the imported alias objects. This lets you capture or pipe the results. Without it, Import-Alias produces no output. This is useful for chaining commands or logging.

alias7.ps1
$imported = Import-Alias -Path "C:\aliases.txt" -PassThru
$imported | Format-Table -AutoSize

This imports aliases and stores them in $imported. Then displays them in a table format. The -PassThru enables this capture and display workflow. Use this for debugging or alias management scripts.

Source

PowerShell documentation

In this article, we have covered the Import-Alias cmdlet in PowerShell.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PowerShell tutorials.