r/PowerShell • u/fa_foon • 9d ago
Question alias ls works but alias doesn't exist anywhere — what is it?
Title:
On PowerShell 5.1 (Windows 11 24H2, build 26100.9711), running alias ls gives valid output:
PS C:\> alias ls
CommandType Name Version Source
----------- ---- ------- ------
Alias ls -> Get-ChildItem
But every way I know to look it up says it doesn't exist:
Get-Alias alias # ItemNotFoundException
Get-Command alias # CommandNotFoundException
Get-Command -Name 'alias' -CommandType Alias, Function, Cmdlet -ErrorAction SilentlyContinue # returns nothing
Get-Item Alias:\alias -ErrorAction SilentlyContinue # returns nothing
No $PROFILE exists. Only standard modules are loaded. What is alias actually doing here?
9
u/joshooaj 9d ago
I think what you're experiencing is the way that PowerShell will assume the verb is "get" if you leave out the verb.
So "alias" is the same as "get-alias" because the "get" is assumed. Try "computerinfo" for example.
4
u/BlackV 9d ago edited 9d ago
For your example you want
Get-Alias lsGet-Command lsGet-Command -Name 'ls' -CommandType Alias, Function, Cmdlet -ErrorAction SilentlyContinueGet-Item Alias:\ls
Alias it's self is term, like an internal function to PowerShell rather than a direct command
Edit: Oh I see what you mean Get- apologies misunderstood, its been well answered now
1
u/narcissisadmin 7d ago
I looked up the word thesaurus in the dictionary and it said "you're being an ass". 🤣
I was amused by this behavior and eventually figure out why it was happening, u/surfingoldelephant explained it well.
1
u/Mr-Fly72 2d ago
And the best example to not trust aliases is the alias command itself ;-)
On Mac/Linux, alias is not defined as an alias, as there is a shell command alias (/usr/bin/alias)
0
u/hemping0 9d ago
tbh i think what you're seeing is that alias is a default alias for Get-Alias loaded at startup, so running alias ls is really just Get-Alias ls. the reason you cant find alias itself is a bit circular, try Get-Alias | Where-Object Name -eq 'alias' and see if it shows up there.
-3
u/jimb2 9d ago
The command ls comes from unix. It was listf for "list files" in earlier operating systems then only the common standard abbreviation ls was transferred to the early unix systems. It remains standard in unix/linux/etc.
PowerShell was designed to work like the various unix command line and scripting shells so incorporated unix commands as aliases where possible. Unix people could migrate with their muscle-memory basic command names intact. The radical and powerful innovation of PowerShell was that the results of functions are well-defined objects, not flat lines of text. That was a beautiful thing!
Powershell also supports the older DOS/Command promt commands as aliases, eg
Get-ChildItem Powershell commandlet full name gci Abbreviated PS alias ls Unix command alias dir Microsoft DOS/Windows command line alias
These can be used interchangeably.
1
u/BlackV 8d ago
That's caught me too, bit that isn't what op was asking
Could be any command, they unfortunately picked an alias AND used
aliasin their examplesThey are asking how
Disk Physicaldisk Timezone Date (probably a bad one too) MemberAnd so work, those are all the cmdlets without
get-In frontPowerShell engine will take any command you type, if it cant find it, it will prepend
get-and try again
20
u/surfingoldelephant 9d ago edited 9d ago
Command discovery tries with
Get-if it can't find a command. You can see it withTrace-Command:Trace-Command -Name CommandDiscovery -PSHost -Expression { alias ls }Which outputs:
It does this for any non-existent command that doesn't have
Verb-, but it's expensive as it has to look inPATHfirst.Get-Commandis missing the logic. See issue #3987.