Bootstrap FreeKB - PowerShell - Run an executable or bat script
PowerShell - Run an executable or bat script

Updated:   |  PowerShell articles

There are a few different ways to go about running an executable (EXE) or bat script in PowerShell.

  • Using the full path to the executable or bat script
  • Using dot forward slash
  • Using Invoke-Expression
  • Using Start-Process

Probably the easiest way to run an executable or bat script is to simply just using the full path to the executable or bat script on the command line. In this example, here is how you would run example.exe.

C:\Users\JohnDoe> C:\temp\example.exe

 

Sometimes, it might be a bit easier to use cd to change directory to the directory that contains the executable or bat script and to then use dot forward slash to run the executable or bat script.

C:\Users\JohnDoe> cd C:\temp\
C:\Users\JohnDoe> ./example.bat

 

Invoke-Expression can be used to run an executable (.exe) file. In this example, here is how you would run example.exe.

C:\Users\JohnDoe> Invoke-Expression -Command example.exe

 

If the executable is not in your present working directory, include the absolute path to the executable.

C:\Users\JohnDoe> Invoke-Expression -Command C:\temp\example.exe

 

If the path to the executable contains whitespacebackticks can be used to escape the whitespace.

C:\Users\JohnDoe> Invoke-Expression -Command C:\temp\my` programs\example.exe

 

If the path to the executable contains parenthesesbackticks can be used to escape the parentheses.

C:\Users\JohnDoe> Invoke-Expression -Command C:\Program` Files` `(x86`)\example.exe

 

If parameters are includes, wrap the command in single quotes.

C:\Users\JohnDoe> Invoke-Expression -Command 'C:\temp\my` programs\example.exe -log my.log'

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 926a16 in the box below so that we can be sure you are a human.