Functions Always Beat Cmdlets

by Nov 11, 2014

All PowerShell versions

Functions always have higher rank than cmdlets, so if both are named alike, the function wins.

This function would effectively change the behavior of Get-Process:

function Get-Process
{
  'go away'
} 

And this is the not-so surprising result:

 
PS> Get-Process
go away 

Even if you specify the fully qualified cmdlet name, functions still win:

function Microsoft.PowerShell.Management\Get-Process
{
  'go away'
} 
 
PS> Microsoft.PowerShell.Management\Get-Process -Id $pid 
go away 

The same applies to Aliases. They rank even above functions.

The only way of making sure you are running the cmdlet would be accessing the module, picking the wanted cmdlet, and directly invoking it:

$module = Get-Module Microsoft.PowerShell.Management
$cmdlet = $module.ExportedCmdlets['Get-Process'] 
& $cmdlet   

Or, simply make sure no one has fiddled with your PowerShell environment, by starting a fresh PowerShell and making sure you use the -noprofile parameter.

Twitter This Tip! ReTweet this Tip!