PowerShell Aliases: Create Shortcuts for Commands

ankitg121 pts0 comments

PowerShell Aliases: Create Shortcuts for Commands • PowerShell Tips

Skip to content

PowerShell Aliases: Create Shortcuts for Commands<br>─□✕

You’ve already been using PowerShell aliases without knowing it — ls, dir, cd, and cls all work in PowerShell, and they’re all built-in aliases. PowerShell aliases are shortcut names that map to full cmdlet names, and you can create your own to reduce repetitive typing. This post covers how to find existing aliases, create new ones, work around the arguments limitation, and persist your custom aliases across sessions.

List All Built-In Aliases

Get-Alias returns all currently defined aliases. PowerShell ships with over 160 built-in aliases for common cmdlets and to provide familiar names for Unix and CMD users.

# List all aliases<br>Get-Alias | Sort-Object Name | Format-Table Name, Definition, Description<br>Name Definition Description<br>? Where-Object<br>% ForEach-Object<br>cat Get-Content<br>cd Set-Location<br>cls Clear-Host<br>cp Copy-Item<br>dir Get-ChildItem<br>echo Write-Output<br>gci Get-ChildItem<br>kill Stop-Process<br>ls Get-ChildItem<br>mv Move-Item<br>pwd Get-Location<br>rm Remove-Item<br>...<br># Find the alias for a specific cmdlet<br>Get-Alias | Where-Object Definition -eq "Get-ChildItem"

# Look up what an alias points to<br>Get-Alias -Name ls<br>Create an Alias with Set-Alias

Set-Alias maps a short name to any cmdlet, function, or executable. The alias only persists for the current session unless you add it to your profile.

# Create simple aliases<br>Set-Alias -Name np -Value notepad<br>Set-Alias -Name gs -Value Get-Service<br>Set-Alias -Name gp -Value Get-Process

# Use the alias immediately<br>gs wuauserv # Same as: Get-Service wuauserv<br># Alias to a function (best for complex shortcuts)<br>function Start-AdminSession { Start-Process pwsh -Verb RunAs }<br>Set-Alias -Name admin -Value Start-AdminSession<br>Aliases with Arguments — Use Functions Instead

PowerShell aliases map a name to a cmdlet only — you cannot include arguments in an alias definition. Set-Alias ll "Get-ChildItem -Force" does not work. For shortcuts that need default arguments, define a function instead.

# WRONG — this does not work<br># Set-Alias ll "Get-ChildItem -Force" # -Force is not accepted here

# CORRECT — use a function wrapper<br>function ll { Get-ChildItem -Force @args }<br>function la { Get-ChildItem -Force -Hidden @args }<br>function lll { Get-ChildItem | Sort-Object LastWriteTime -Descending | Select-Object -First 20 }<br>Using @args (the splatted args automatic variable) passes any additional arguments you supply through to the underlying cmdlet.

Remove an Alias

Use Remove-Alias (PowerShell 6.1+) or Remove-Item Alias:\aliasname on older versions to delete an alias from the current session.

# PowerShell 6.1+<br>Remove-Alias -Name gs

# Older PowerShell / Windows PowerShell<br>Remove-Item -Path Alias:\gs

# Verify it's gone<br>Get-Alias -Name gs -ErrorAction SilentlyContinue<br>Export and Import Aliases

Export your current aliases to a CSV and reimport them in another session. This is useful for sharing your alias configuration or for backup purposes.

# Export all current aliases to CSV<br>Export-Alias -Path "C:\Logs\my-aliases.csv" -Force

# Import aliases from file in a new session<br>Import-Alias -Path "C:\Logs\my-aliases.csv" -Force<br>Persist Aliases in Your Profile

Aliases created with Set-Alias only last for the current session. To make them permanent, add them to your PowerShell profile file so they’re created automatically at every session start.

# Open your profile for editing<br>notepad $PROFILE

# Add these lines to your profile:<br># Set-Alias -Name np -Value notepad<br># Set-Alias -Name gs -Value Get-Service<br># function ll { Get-ChildItem -Force @args }<br># function touch { New-Item -ItemType File -Name $args[0] }<br>After saving, the aliases will be available in every new PowerShell session automatically.

Common Errors and Fixes

Aliases cannot include arguments: This is the most common confusion with PowerShell aliases. Set-Alias myls "Get-ChildItem -la" will create an alias that points to the literal string "Get-ChildItem -la" as a command name, which fails at runtime. The solution is always a function: function myls { Get-ChildItem -Force $args } — functions fully support arguments.

Session-only unless added to profile: If your custom aliases disappear when you close and reopen PowerShell, you forgot to add them to your $PROFILE file. Anything created interactively only persists for that session. Profile location: run $PROFILE to see the path, then open it with notepad $PROFILE and add your alias definitions.

Related Cmdlets / See Also

PowerShell Profile: Create a Custom Profile Script

PowerShell Functions Tutorial

Wrapping Up

Aliases save keystrokes for the commands you run most often, and function wrappers extend that to shortcuts with built-in default arguments. As a next step, open your profile with notepad $PROFILE and add five aliases or function shortcuts for the cmdlets you use every day — you’ll notice the improvement immediately.

More from this author:...

alias aliases powershell name profile function

Related Articles