Where are the default aliases defined in PowerShell?
They are "built in" but not immutable. Note:
PS > (Get-Alias dir).Options AllScope PS > (Get-Alias gci).Options ReadOnly, AllScopePS > 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 > slDirectory: C:\Users\Jay
Mode LastWriteTime Length Name ---- ------------- ------ ----
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
...
;-)