Home Best Practice Commands Add .EXE Extension to Applications
 

Add .EXE Extension to Applications

For PowerShell, applications are equal citizen, and as long as an exe does what you want it to, it can be easily blended into your PowerShell scripts.

For example, this piece of code would use whoami.exe to find out whether your account (the one that runs the PowerShell script) has elevated privileges or not:

  

function Test-Admin
{
  $result = (whoami.exe /all) -like '*S-1-16-8192*'
  $result.Count -eq 0
}

  Test-Admin returns $true for elevated accounts, else $false

Adding .EXE Extension to Applications

What you should do is add the appropriate extension to the command. So in your scripts, write "whoami.exe" and not just "whoami".

While you would get away with not using the .EXE extension, it is good practice to add it anyway for a number of reasons:

Adding Clarity

Adding the .EXE extension lets you identify external applications immediately, so you are not left wondering whether they are functions, cmdlets, or applications.

Adding Robustness

Applications rank very low in the list of command types: aliases, functions, and cmdlets all rank higher. So if there was an alias, a function, or a cmdlet that used the same name, PowerShell would use that one instead of the application.

PS> whoami
tobi2\tobias

PS> function whoami { "I am a function!" }

PS> whoami
I am a function!

PS> whoami.exe
tobi2\tobias

PS> 

  The extension .EXE adds some protection for application calls

While it is unlikely that a cmdlet would ever use an application name by accident (thanks to the strict Verb-Noun syntax), there is a chance that you may accidentally name a function like an application.

So by adding the .EXE extension, chances are much lower that you accidentally "overwrite" applications by defining functions or aliases with the same name.