r/PowerShell • • 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?

7 Upvotes

10 comments sorted by

20

u/surfingoldelephant 9d ago edited 9d ago

Command discovery tries with Get- if it can't find a command. You can see it with Trace-Command:

Trace-Command -Name CommandDiscovery -PSHost -Expression { alias ls }

Which outputs:

DEBUG: CommandDiscovery Information: 0 : Looking up command: alias
DEBUG: CommandDiscovery Information: 0 : PATH: [...]
DEBUG: CommandDiscovery Information: 0 : Looking for alias.* in C:\WINDOWS\system32
[...]
DEBUG: CommandDiscovery Information: 0 : The command [alias] was not found, trying again with get- prepended
DEBUG: CommandDiscovery Information: 0 : Looking up command: get-alias
DEBUG: CommandDiscovery Information: 0 : Cmdlet found: Get-Alias  Microsoft.PowerShell.Commands.GetAliasCommand

It does this for any non-existent command that doesn't have Verb-, but it's expensive as it has to look in PATH first.

Get-Command is missing the logic. See issue #3987.

3

u/fa_foon 9d ago

thanks i didn't know that

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 ls
Get-Command ls
Get-Command -Name 'ls' -CommandType Alias, Function, Cmdlet -ErrorAction SilentlyContinue
Get-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 alias in their examples

They are asking how

Disk
Physicaldisk
Timezone
Date (probably a bad one too)
Member

And so work, those are all the cmdlets without get- In front

PowerShell engine will take any command you type, if it cant find it, it will prepend get- and try again