How to Run an Executable in PowerShell using Start-Process?

Do you ever find yourself needing to launch an executable file from the Windows PowerShell command line? Running executable files in PowerShell can be a powerful and efficient way to automate tasks and perform various operations on your Windows system. Whether you’re a system administrator or a PowerShell enthusiast, understanding the different methods to run executable files can greatly enhance your productivity. This comprehensive guide will explore the various ways to execute .exe files using PowerShell, providing clear instructions and practical examples.

powershell run executable

As a system administrator, I have always found the PowerShell Start-Process command to be an incredibly useful tool. It allows starting programs, scripts, and executables from within PowerShell, providing greater control and flexibility over the system. In this guide, I will show you everything you need to know about using the PowerShell Start-Process command.

Introduction to PowerShell’s Start-Process Command

The PowerShell Start-Process command is a powerful tool that is used to start a new program or script in a new process. This cmdlet allows us to launch external programs directly from the command line or a PowerShell script and can be used to start any program, including batch files, executables, and PowerShell scripts.

One of the biggest advantages of using the Start-Process command is that it allows you to run programs as a different user or with elevated privileges. This means that you can run programs with administrator privileges, even if you are logged in as a standard user. Additionally, the Start-Process command allows you to pass arguments to the program or script that you are starting, which gives you even greater control over the process.

Understanding the Start-Process Command

Run an Executable in PowerShell

The PowerShell Start-Process command has a number of different parameters that you can use to customize the way that the command runs. The most important parameter is the FilePath parameter, which specifies the location of the program or script that you want to start.

Another important parameter is the ArgumentList parameter, which allows you to pass arguments to the program or script you are starting. For example, if you are starting a PowerShell script, you can pass in parameters to the script using the ArgumentList parameter.

The Start-Process command also has parameters that allow you to specify the working directory, the window style, and the priority of the process that you are starting. These parameters can be useful if you need to start a program or script in a specific way. Along with other default parameters, Here is the list of important parameters of start-process cmdlet:

ParameterDescription
-FilePathSpecify the executable, application, file, batch, or script to run
-ArgumentListSpecifies parameters to use with the process to start
-CredentialUser account to run the process
-NoNewWindowOpen a window in the current console process
-PassthruReturns the process object of the process started. You can get the process ID from it.
-RedirectStandardErrorSpecify text file to redirect error output to
-RedirectStandardInputText file with input for the process
-RedirectStandardOutputSpecify text file to redirect output to
-UseNewEnvironmentThe process will use new environment variables specified for the process instead default, under: Machine and user
-WindowStyleSpecifies the state of the window: Normal, Hidden, Minimized, or Maximized
-LoadUserProfileLoads the Windows user profile from the HKEY_USERS registry key for the current user. This does not affect the PowerShell profiles.
-WaitWait for the process to complete before continuing with the script
-WorkingDirectoryThe location where the process should start in
CommonParametersVerbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable

Running Programs with PowerShell’s Start-Process Command

One of the most common uses of the Microsoft PowerShell Start-Process command is to start programs. To start a program, you simply need to specify the location of the program using the FilePath parameter. For example, to start Notepad from a PowerShell terminal or PowerShell ISE, you would use the following command:

Start-Process -FilePath "notepad.exe"

This command would start Notepad in a new process.

PowerShell Run Exe

If you want to run an executable in the background without displaying any windows or prompts, you can use the -WindowStyle Hidden parameter of the Start-Process cmdlet. Similarly, you can open any specified file (Non-executable file, such as a doc file) in a new window using:

Start-Process -FilePath "C:\Docs\AppLog.txt" -WindowStyle Maximized

This opens the file “C:\Docs\AppLog.txt” in a maximized window.

Running Programs with Elevated Privileges using Start-Process Command

Another useful feature of the PowerShell Start-Process command is the ability to run programs with elevated privileges. This is particularly useful if you need to run a program as an administrator, but you are logged in as a standard user. To run a program with elevated privileges, you can use the RunAs parameter.

For example, to start PowerShell as an administrator, you would use the following command:

Start-Process -FilePath "powershell.exe" -Verb RunAs

This command would start PowerShell from the current user’s context with elevated privileges. The -Verb parameter can have other actions, such as “Print” to trigger print from the application.

Start-Process -FilePath "C:\Temp\Log.txt" -Verb Print

Running executables as a different user with Start-Process in PowerShell

The start-Process cmdlet users the credentials of the current user. However, In certain scenarios, you may need to run executables as a different user in PowerShell. This can be useful when you want to execute a program with elevated privileges or when you need to run a program on behalf of another user. PowerShell provides us with the ability to run executables as a different user using the Start-Process cmdlet. Let’s consider an example:

Start-Process -FilePath "powershell.exe" -Credential (Get-Credential)

In the above example, we utilize the Credential parameter of the Start-Process cmdlet and Get-Credential cmdlet to specify the credentials of the user under which we want to run the executable. PowerShell prompts us to enter the username and password, ensuring secure execution of the executable with the specified user’s privileges.

powershell start-process

You can also run an application through a script file from a saved PSCredential object with a hard-coded user name and password:

# Parameters
$FilePath = "C:\Scripts\RunMe.bat"
$UserName = "Crescent\salaudeen"
$Password = "Password goes here"
 
#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword

#Start a process with 
Start-Process -FilePath $FilePath -Wait -Credential $Credential

Running PowerShell Scripts with Start-Process Command

The PowerShell Start-Process command can also be used to start PowerShell scripts. To start a PowerShell script, you need to specify the location of the script using the FilePath parameter. Additionally, you can pass arguments to the script using the ArgumentList parameter.

For example, to start a PowerShell script that is located in C:\Scripts\MyScript.ps1, you would use the following command:

Start-Process -FilePath "powershell.exe" -ArgumentList "-File C:\Scripts\MyScript.ps1"

This command would start the MyScript.ps1 script in a new PowerShell process.

Using Arguments with Start-Process Command

As mentioned earlier, the PowerShell Start-Process command allows you to pass arguments to the program or script you are starting. This can be useful if you need to customize the behavior of the program or script.

For example, if you are starting a program that requires command-line arguments, you can pass those arguments using the ArgumentList parameter.

Start-Process -FilePath "myprogram.exe" -ArgumentList "-arg1 value1", "-arg2 value2"

This command would start the myprogram.exe program with the arguments “-arg1 value1” and “-arg2 value2”. The parameter values depend on the application you are running. Let’s take an example: Suppose you want to open a URL in Google Chrome. Here’s how you can do it:

Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "https://www.google.com" -Wait -WindowStyle Maximized
start-process argument

In this example, the -FilePath parameter specifies the path to the executable file for Chrome and the -ArgumentList parameter provides the URL to open in Chrome. The arguments are passed as a string, where a space separates each argument. It also uses a wait switch to wait for the process to close and Windowstyle parameters. This enables us to provide multiple args to the executable and customize its behavior.

Running Executables with Start-Process Command

Running executables with Start-Process in PowerShell is a straightforward process. By simply specifying the path of the executable as an argument to the Start-Process cmdlet, PowerShell launches the program and executes it. This method is particularly useful when you want to run standalone executables without any additional parameters or customization. Let’s take a look at an example:

To start an executable, you need to specify the location of the executable using the FilePath parameter.

Start-Process -FilePath "C:\MyProgram\MyProgram.exe"

This command would start the MyProgram.exe executable.

Running Batch Files with Start-Process Command

PowerShell is not limited to running standalone executables only; it can also execute scripts and batch files effortlessly. By utilizing the Start-Process cmdlet, we can launch scripts and batch files in PowerShell and automate complex tasks. This provides us with the flexibility to integrate PowerShell with other scripting languages and leverage their capabilities. Let’s take a look at an example of running a PowerShell script using Start-Process:

To start a batch file, you need to specify the location of the batch file using the FilePath parameter.

Start-Process -FilePath "C:\MyBatchFile.bat"

This command would start the MyBatchFile.bat file. Apart from the Start-Process cmdlet, We have the Invoke-Expression cmdlet, using the call operator (&) and .Net framework methods to run executables from PowerShell. Read more at: How to Run a Batch File from PowerShell?

Common Errors and Troubleshooting Tips with Start-Process Command

While the PowerShell Start-Process command is a powerful tool, there are a few common errors that you may encounter.

  1. Access is denied: One common error is the “Access is denied” error. This error occurs when you try to run a program or script with elevated privileges, but you do not have permission to do so. To fix this error, you will need to log in as an administrator or use the RunAs parameter to run the program with elevated privileges.
  2. File not found: Another common error is the “File not found” error. This error occurs when you specify an incorrect file path. To fix this error, you will need to double-check the file path and make sure that it is correct. This includes verifying file permissions and network access if the executable resides on a remote machine. Always use the full path of the executable to avoid any ambiguity or reliance on the system’s PATH environment variable.
  3. Invalid Arguments: If the executable requires specific arguments, ensure that they are passed correctly using the ArgumentList parameter of the Start-Process cmdlet.

Use error-handling techniques such as Try-Catch blocks to gracefully handle any exceptions that may occur during the execution of the executable. By identifying and resolving these common issues, you can overcome any obstacles that may arise while running executables in PowerShell and ensure smooth execution.

Conclusion and Best Practices

In this guide, we have covered everything you need to know about using the PowerShell Start-Process command. As you can see, this command is a powerful tool that can be used to start programs, scripts, and executables in a new process. By understanding the Start-Process command and its parameters, you can automate tasks, run batch files, execute programs with elevated privileges, pass arguments, and even run executables as a different user.

When using the Start-Process command, it is important to double-check your file paths and arguments to avoid common errors. Additionally, it is a good practice to test your commands in a test environment before running them in a production environment.

Now that you have a better understanding of the PowerShell Start-Process command, you can start using it to automate your tasks and streamline your workflow!

How do I run a PowerShell script like an EXE?

To run a PowerShell script like an EXE, you can create a batch file that calls the PowerShell script. In the batch file, you would use the following command: “powershell.exe -ExecutionPolicy Bypass -File C:\Path\to\Script.ps1”. This will bypass the execution policy and execute the PowerShell script just like an EXE file.

How do I run an EXE file in PowerShell?

To run an EXE file in PowerShell, you can use the “Start-Process” cmdlet. Here’s an example of the command you can use: “Start-Process -FilePath ‘C:\Path\to\file.exe'”. Replace ‘C:\Path\to\file.exe’ with the actual path to your EXE file.

How do I install an EXE file in PowerShell silently?

To install an EXE file silently using PowerShell, you can use the Start-Process cmdlet with the -ArgumentList parameter. Here’s an example of the command you can use: Start-Process -FilePath “path\to\file.exe” -ArgumentList “/silent”

How do I open an application in PowerShell?

To open an application using PowerShell, you can use the Start-Process cmdlet followed by the path to the application executable. For example, to open Notepad, you can use the command Start-Process notepad.exe. in the PowerShell console.

How do I run an EXE file as an administrator in PowerShell?

To run an EXE file as an administrator in PowerShell, you can use the Start-Process cmdlet with the -Verb parameter set to “RunAs”. Here’s an example command: Start-Process -FilePath “C:\Path\to\file.exe” -Verb RunAs

How to run an exe file from the command line?

To run an EXE file from the command line, you need to navigate to the directory where the file is located using the “cd” command. Once you are in the current directory, you can simply type the name of an executable file and press enter to run it.

How do I run PS from the command prompt?

To run PowerShell commands from the command prompt, you can use the “PowerShell” command followed by the desired PowerShell command name. For example, to run the “Get-Process” command in PowerShell from the cmd, you would type “powershell Get-Process” and press Enter.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *