Xerion
Xerion

Reputation: 3281

Where are the default aliases defined in PowerShell?

This may be a stupid question, but are the default aliases (e.g. cd) hardcoded in PowerShell or defined in a hidden "profile" script somewhere?

I don't have any profiles set (per-user or system-wide) so I'm just wondering where the default ones come from.

Upvotes: 9

Views: 4489

Answers (6)

postanote
postanote

Reputation: 16076

Also of note, there are two places to be aware of aliases. the global ones and the cmdlet parameter specific ones.

# Get named aliases 
Get-Alias | 
Out-GridView -PassThru -Title 'Available aliases'

# Get cmdlet / function parameter aliases
(Get-Command Get-ADUser).Parameters.Values | 
where aliases | 
select Name, Aliases | 
Out-GridView -PassThru -Title 'Alias results for a given cmdlet or function.'

Upvotes: 0

YenForYang
YenForYang

Reputation: 3284

Another way to find the builtin aliases by using the (C#) source code from GitHub:

https://github.com/PowerShell/PowerShell/blob/98cf44cdecd72ffc328c3b7ca216fae20a08f1e4/src/System.Management.Automation/engine/InitialSessionState.cs#L4549-L4714

internal static SessionStateAliasEntry[] BuiltInAliases {
    get {
            // Too many AllScope entries hurts performance because an entry is
            // created in each new scope, so we limit the use of AllScope to the
            // most commonly used commands - primarily so command lookup is faster,
            // though if we speed up command lookup significantly, then removing
            // AllScope for all of these aliases makes sense.

            const ScopedItemOptions AllScope = ScopedItemOptions.AllScope;
            const ScopedItemOptions ReadOnly_AllScope = ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope;
            const ScopedItemOptions ReadOnly = ScopedItemOptions.ReadOnly;

            return new SessionStateAliasEntry[] {
                new SessionStateAliasEntry("foreach", "ForEach-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("%", "ForEach-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("where", "Where-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("?", "Where-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("clc", "Clear-Content", string.Empty, ReadOnly),
                new SessionStateAliasEntry("cli", "Clear-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("clp", "Clear-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("clv", "Clear-Variable", string.Empty, ReadOnly),
                new SessionStateAliasEntry("cpi", "Copy-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("cvpa", "Convert-Path", string.Empty, ReadOnly),
                new SessionStateAliasEntry("dbp", "Disable-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ebp", "Enable-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("epal", "Export-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("epcsv", "Export-Csv", string.Empty, ReadOnly),
                new SessionStateAliasEntry("fl", "Format-List", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ft", "Format-Table", string.Empty, ReadOnly),
                new SessionStateAliasEntry("fw", "Format-Wide", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gal", "Get-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gbp", "Get-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gc", "Get-Content", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gci", "Get-ChildItem", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gcm", "Get-Command", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gdr", "Get-PSDrive", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gcs", "Get-PSCallStack", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ghy", "Get-History", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gi", "Get-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gl", "Get-Location", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gm", "Get-Member", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gmo", "Get-Module", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gp", "Get-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gpv", "Get-ItemPropertyValue", string.Empty,ReadOnly),
                new SessionStateAliasEntry("gps", "Get-Process", string.Empty, ReadOnly),
                new SessionStateAliasEntry("group", "Group-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gu", "Get-Unique", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gv", "Get-Variable", string.Empty, ReadOnly),
                new SessionStateAliasEntry("iex", "Invoke-Expression", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ihy", "Invoke-History", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ii", "Invoke-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ipmo", "Import-Module", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ipal", "Import-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ipcsv", "Import-Csv", string.Empty, ReadOnly),
                new SessionStateAliasEntry("measure", "Measure-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("mi", "Move-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("mp", "Move-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("nal", "New-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ndr", "New-PSDrive", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ni", "New-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("nv", "New-Variable", string.Empty, ReadOnly),
                new SessionStateAliasEntry("nmo", "New-Module", string.Empty, ReadOnly),
                new SessionStateAliasEntry("oh", "Out-Host", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rbp", "Remove-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rdr", "Remove-PSDrive", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ri", "Remove-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rni", "Rename-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rnp", "Rename-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rp", "Remove-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rmo", "Remove-Module", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rv", "Remove-Variable", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rvpa", "Resolve-Path", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sal", "Set-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sbp", "Set-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("select", "Select-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("si", "Set-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sl", "Set-Location", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sp", "Set-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("saps", "Start-Process", string.Empty, ReadOnly),
                new SessionStateAliasEntry("spps", "Stop-Process", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sv", "Set-Variable", string.Empty, ReadOnly),
                // Web cmdlets aliases
                new SessionStateAliasEntry("irm", "Invoke-RestMethod", string.Empty, ReadOnly),
                new SessionStateAliasEntry("iwr", "Invoke-WebRequest", string.Empty, ReadOnly),
// Porting note: #if !UNIX is used to disable aliases for cmdlets which conflict with Linux / macOS
#if !UNIX
                // ac is a native command on macOS
                new SessionStateAliasEntry("ac", "Add-Content", string.Empty, ReadOnly),
                new SessionStateAliasEntry("compare", "Compare-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("cpp", "Copy-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("diff", "Compare-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gsv", "Get-Service", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sleep", "Start-Sleep", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sort", "Sort-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("start", "Start-Process", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sasv", "Start-Service", string.Empty, ReadOnly),
                new SessionStateAliasEntry("spsv", "Stop-Service", string.Empty, ReadOnly),
                new SessionStateAliasEntry("tee", "Tee-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("write", "Write-Output", string.Empty, ReadOnly),
                // These were transferred from the "transferred from the profile" section
                new SessionStateAliasEntry("cat", "Get-Content"),
                new SessionStateAliasEntry("cp", "Copy-Item", string.Empty, AllScope),
                new SessionStateAliasEntry("ls", "Get-ChildItem"),
                new SessionStateAliasEntry("man", "help"),
                new SessionStateAliasEntry("mount", "New-PSDrive"),
                new SessionStateAliasEntry("mv", "Move-Item"),
                new SessionStateAliasEntry("ps", "Get-Process"),
                new SessionStateAliasEntry("rm", "Remove-Item"),
                new SessionStateAliasEntry("rmdir", "Remove-Item"),
                new SessionStateAliasEntry("cnsn", "Connect-PSSession", string.Empty, ReadOnly),
                new SessionStateAliasEntry("dnsn", "Disconnect-PSSession", string.Empty, ReadOnly),
#endif
                // Bash built-ins we purposefully keep even if they override native commands
                new SessionStateAliasEntry("cd", "Set-Location", string.Empty, AllScope),
                new SessionStateAliasEntry("dir", "Get-ChildItem", string.Empty, AllScope),
                new SessionStateAliasEntry("echo", "Write-Output", string.Empty, AllScope),
                new SessionStateAliasEntry("fc", "Format-Custom", string.Empty, ReadOnly),
                new SessionStateAliasEntry("kill", "Stop-Process"),
                new SessionStateAliasEntry("pwd", "Get-Location"),
                new SessionStateAliasEntry("type", "Get-Content"),
                // Native commands we keep because the functions act correctly on Linux
                new SessionStateAliasEntry("clear", "Clear-Host"),
// #if !CORECLR is used to disable aliases for cmdlets which are not available on OneCore or not appropriate for PSCore6 due to conflicts
#if !CORECLR
                new SessionStateAliasEntry("gwmi", "Get-WmiObject", string.Empty, ReadOnly),
                new SessionStateAliasEntry("iwmi", "Invoke-WMIMethod", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ogv", "Out-GridView", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ise", "powershell_ise.exe", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rwmi", "Remove-WMIObject", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sc", "Set-Content", string.Empty, ReadOnly),
                new SessionStateAliasEntry("swmi", "Set-WMIInstance", string.Empty, ReadOnly),
                new SessionStateAliasEntry("shcm", "Show-Command", string.Empty, ReadOnly),
                new SessionStateAliasEntry("trcm", "Trace-Command", string.Empty, ReadOnly),
                new SessionStateAliasEntry("lp", "Out-Printer"),
#endif
                // Aliases transferred from the profile
                new SessionStateAliasEntry("h", "Get-History"),
                new SessionStateAliasEntry("history", "Get-History"),
                new SessionStateAliasEntry("md", "mkdir", string.Empty, AllScope),
                new SessionStateAliasEntry("popd", "Pop-Location", string.Empty, AllScope),
                new SessionStateAliasEntry("pushd", "Push-Location", string.Empty, AllScope),
                new SessionStateAliasEntry("r", "Invoke-History"),
                new SessionStateAliasEntry("cls", "Clear-Host"),
                new SessionStateAliasEntry("chdir", "Set-Location"),
                new SessionStateAliasEntry("copy", "Copy-Item", string.Empty, AllScope),
                new SessionStateAliasEntry("del", "Remove-Item", string.Empty, AllScope),
                new SessionStateAliasEntry("erase", "Remove-Item"),
                new SessionStateAliasEntry("move", "Move-Item", string.Empty, AllScope),
                new SessionStateAliasEntry("rd", "Remove-Item"),
                new SessionStateAliasEntry("ren", "Rename-Item"),
                new SessionStateAliasEntry("set", "Set-Variable"),
                new SessionStateAliasEntry("icm", "Invoke-Command"),
                new SessionStateAliasEntry("clhy", "Clear-History", string.Empty, ReadOnly),
                // Job Specific aliases
                new SessionStateAliasEntry("gjb", "Get-Job"),
                new SessionStateAliasEntry("rcjb", "Receive-Job"),
                new SessionStateAliasEntry("rjb", "Remove-Job"),
                new SessionStateAliasEntry("sajb", "Start-Job"),
                new SessionStateAliasEntry("spjb", "Stop-Job"),
                new SessionStateAliasEntry("wjb", "Wait-Job"),
#if !CORECLR
                new SessionStateAliasEntry("sujb", "Suspend-Job"),
                new SessionStateAliasEntry("rujb", "Resume-Job"),
                // Remoting Cmdlets Specific aliases
                new SessionStateAliasEntry("npssc", "New-PSSessionConfigurationFile", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ipsn", "Import-PSSession"),
                new SessionStateAliasEntry("epsn", "Export-PSSession"),
#endif
                new SessionStateAliasEntry("nsn", "New-PSSession"),
                new SessionStateAliasEntry("gsn", "Get-PSSession"),
                new SessionStateAliasEntry("rsn", "Remove-PSSession"),
                new SessionStateAliasEntry("etsn", "Enter-PSSession"),
                new SessionStateAliasEntry("rcsn", "Receive-PSSession", string.Empty, ReadOnly),
                new SessionStateAliasEntry("exsn", "Exit-PSSession"),
                // Win8: 121662/169179 Add "sls" alias for Select-String cmdlet
                //   - do not use AllScope - this causes errors in profiles that set this somewhat commonly used alias.
                new SessionStateAliasEntry("sls", "Select-String"),
            };
        }
}

Of course the above aliases are subject to change, but accessing them this way may be more convenient/informative in some situations.

Upvotes: 4

x0n
x0n

Reputation: 52410

Hardcoded, but retrievable (like most things "hidden" in PowerShell)

PS> [Management.Automation.Runspaces.InitialSessionState].getproperty(
        "BuiltInAliases", [reflection.bindingflags]"NonPublic,Static").getvalue(
             $null, @()) | format-table -auto

Definition           Description            Options CommandType Visibility Name    PSSnapIn Module
----------           -----------            ------- ----------- ---------- ----    -------- ------
Add-Content                      ReadOnly, AllScope       Alias     Public ac
Add-PSSnapIn                     ReadOnly, AllScope       Alias     Public asnp
Clear-Content                    ReadOnly, AllScope       Alias     Public clc
Clear-Item                       ReadOnly, AllScope       Alias     Public cli
Clear-ItemProperty               ReadOnly, AllScope       Alias     Public clp
Clear-Variable                   ReadOnly, AllScope       Alias     Public clv
...

;-)

Upvotes: 7

Jay Bazuzi
Jay Bazuzi

Reputation: 46496

They are "built in" but not immutable. Note:

PS >  (Get-Alias dir).Options
AllScope
PS >  (Get-Alias gci).Options
ReadOnly, AllScope

PS > Get-Alias | group Options

Count Name Group ----- ---- ----- 91 ReadOnly, AllScope {%, ?, ac, asnp...} 46 AllScope {cat, cd, chdir, clear...}

As you can see, there is some partitioning of aliases by the ReadOnly option. The ReadOnly ones are idiomatic in PowerShell, while the mutable ones are for people familiar with other shells. I've seen people modify dir to add more functionality, while keeping gci as a straight alias to Get-ChildItem.

For broad compatability, I only use the ReadOnly aliases in my scripts.

Also, because dir in CMD, ls in UNIX, and gci in PowerShell each work in their own way, I train myself to use the native command, and not an alias. dir tends to work everywhere, but dir -Recurse does not!

As a training exercise, and to test my scripts for compatibility, I sometimes remove the non-ReadOnly aliases:

Get-Alias | ? { ! ($_.Options -match "ReadOnly") } | % { Remove-Item alias:$_ }

There's a more gentle approach where you replace each alias with a new command that warns you that you're using one of the compatibility aliases, but lets you keep functioning.

Also, you can change the ReadOnly aliases if you really want to, but for the above reasons I'd recommend against it:

PS >  Set-Alias -Name sl -Value Get-ChildItem -Force -Option AllScope     # BAD!
PS >  sl

Directory: C:\Users\Jay

Mode LastWriteTime Length Name ---- ------------- ------ ----

Upvotes: 10

Shay Levy
Shay Levy

Reputation: 126712

They are hardcoded.

Upvotes: 0

Roman Kuzmin
Roman Kuzmin

Reputation: 42035

Though I do not know the technical details I would say they are hardcoded and they are not configurable. They can be redefined or removed but the initial set is not under our control.

Upvotes: 1

Related Questions