Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the default aliases defined in PowerShell?

Tags:

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.

like image 892
Xerion Avatar asked May 05 '10 03:05

Xerion


1 Answers

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 ---- ------------- ------ ----

like image 101
Jay Bazuzi Avatar answered Oct 21 '22 15:10

Jay Bazuzi