Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does powershell resolve command names?

Tags:

powershell

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.

like image 830
MarkPflug Avatar asked Mar 07 '14 19:03

MarkPflug


1 Answers

From help about_Command_Precedence:

If you do not specify a path, Windows PowerShell uses the following
precedence order when it runs commands:
     1. Alias
     2. Function
     3. Cmdlet
     4. Native Windows commands

Also,

When the session contains items of the same type that have the same   
name, such as two cmdlets with the same name, Windows PowerShell      
runs the item that was added to the session most recently.  

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.

# Add '.exe' for executables
log.exe 'This is my log message.'

# Specify the module name
MyModule\log 'This is my log message.'

# Specify alias vs function
&(gi alias:\log) 'This is my log message.'
&(gi function:\log) 'This is my log message.'
like image 75
Rynant Avatar answered Sep 26 '22 07:09

Rynant