PowerShell别名存储位置及自定义永久别名添加方法
Nice question! Let's break this down clearly so you can find those preset aliases and add your own permanent ones easily.
PowerShell's preset aliases fall into two main categories, each with different storage behavior:
Built-in Read-Only Aliases
These are core aliases likedir(mapped toGet-ChildItem),ls, andcdthat are baked directly into the PowerShell engine. They aren't stored in any physical file—instead, they're loaded automatically when PowerShell starts up. You can spot them by running:Get-Alias | Where-Object { $_.Options -match "ReadOnly" }These aliases can't be modified or deleted since they're part of PowerShell's default system configuration.
Profile-Based Preset Aliases
Some preset aliases might be defined in system-wide or user-specific PowerShell profile files. These files run automatically when PowerShell starts, and they can include customizations set by your system admin or previous configurations. Common profile paths are:- System-wide (all users):
$PSHome\Profile.ps1and$PSHome\Microsoft.PowerShell_profile.ps1 - User-specific (current user):
$HOME\Documents\PowerShell\Profile.ps1and$HOME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Note: For older PowerShell versions (5.1 and below), the user profile path is$HOME\Documents\WindowsPowerShellinstead.
- System-wide (all users):
Temporary aliases (created with Set-Alias in a single session) disappear when you close PowerShell. To make them stick across sessions, you need to add them to your PowerShell profile. Here's how:
Check if your profile exists
Run this command to verify if you have a user-specific profile already:Test-Path $PROFILEIf it returns
False, create the profile file with:New-Item -Path $PROFILE -ItemType File -ForceAdd your alias definition
Open the profile file in your preferred editor (Notepad works for quick edits):notepad $PROFILEUse either
Set-Alias(overwrites existing aliases if they exist) orNew-Alias(throws an error if the alias already exists) to define your alias. Examples:- Map
npto open Notepad:Set-Alias -Name np -Value notepad.exe - Map
llto run a detailed directory listing:Set-Alias -Name ll -Value Get-ChildItem -ArgumentList "-Force", "-Recurse"
- Map
Apply the changes
Save the profile file, then either:- Restart PowerShell, or
- Run this command to load the updated profile in your current session:
. $PROFILE
Verify your alias
RunGet-Alias <your-alias-name>(e.g.,Get-Alias np) to confirm it's now available and persistent across sessions.
Quick note: If you want to add aliases for all users on the system, edit the system-wide profile files instead—but you'll need administrator privileges to do that.
内容的提问来源于stack exchange,提问作者lewis4u

