I'm trying to figure out how powershell resolves names, but I can't seem to find any information.
Here is the scenario:
There exists an executable:
c:\stuff\log.exe
The path is set so $env:path = 'c:\stuff\'
I have a module loaded that includes a function name "log" and a binary cmdlet aliased as 'log'.
When I type "log" at the command line, how does PowerShell decide whether to execute c:\stuff\log.exe or the function name log, or the cmdlet alieased as log?
From experimentation it seems the resolution order is: Cmdlet Function executable on the path
But I can't find anything that documents this.
If you want to find out the order Powershell looks for a command try using the
trace-command
cmdlet. For example:is nice and short, but:
on my system produces more than 1,000 lines of output as it searches for a non-existent log command.
The order seems to be basically expand aliases then find functions, cmdlets then search your path for the command, then do it all again with
get-
prepended.The language reference is fairly terse on this, but it does say:
If it mentions that when commands aren't found it tries again with 'get-' prepended I haven't found that bit.
From
help about_Command_Precedence
:Also,
Calling commands with the same name
about_Command_Precedence
also goes into detail about how to explicitly call commands with the same name.Here are some ways to call a
log
command from different sources.To complement the great existing answers:
A simple, pragmatic way of determining what a given command name will execute and if there are other, shadowed commands with the same name:
All commands with the given name will be listed in descending order of precedence, i.e., the effective command will be listed first.
For instance, Windows PowerShell has a built-in
sc
alias for theSet-Content
cmdlet, which shadows the nativesc.exe
(service-control) program (unless you invoke it assc.exe
):