Run Batch File from PowerShell

In Windows PowerShell, we can run batch files in multiple ways.

  • Running the batch file by specifying the absolute or relative path using the ampersand (&) operator:

  • Running the batch file by using the Invoke-Expression cmdlet. This command works similarly with the ampersand operator (&), but we write it in PowerShell flavor.

  • Running the batch file by calling the command prompt executable file:

  • Running the batch file by using the Start-Process cmdlet. Like our other PowerShell command, this cmdlet works similarly when we call an executable file, but we write it in PowerShell flavor. This command also accepts arguments if we pass variables to the file.

Ways to Run Batch Files in PowerShell

PowerShell is one of the modern scripting methods, especially on a Windows operating system-based platform or machine. However, sometimes, despite the modernization, there are still some menial tasks where legacy scripting languages have the advantage. One example of this is these legacy scripting procedures are called Batch scripts.

We can easily incorporate Batch scripts inside PowerShell as Windows built both scripting languages on the same platform. This article will discuss how we can run and include Batch scripts inside PowerShell.

There are several ways to run a batch file (BAT) or script inside the PowerShell environment.

Running the BAT file Using the Absolute and Relative Path

For our first method, we can run the BAT file by just calling their file path or location. To do this, follow the steps below:

  1. Open PowerShell (preferably in administrator mode).
  2. Let’s navigate to the directory where we have placed our batch file or script. We can use the check directory or cd command to navigate. For this example, let’s create and cd to the C:\Scripts directory and locate the example batch file.
  3. Run the batch file or script by typing its name and pressing Enter. For example, if we create a batch file named testPS.bat, use the following command:

Alternatively, we can run a batch file or script by specifying its relative or absolute path in the PowerShell command. For example, to run a batch file located in the C:\Scripts directory, we can use either of the following commands:

By doing it this way, we do not need to manually cd into the directory itself. Note that we use the .\ syntax to specify a relative path, while the full path specifies an absolute one.

However, the method above would be helpful if we run the BAT file inside the PowerShell command line terminal. However, what if we incorporate the BAT file inside a PowerShell file, perhaps inside PowerShell ISE?

We can use the ampersand (&) operator and prefix it to our BAT file paths. However, it doesn’t do any additional functions, as its primary function is to run commands. However, it is a good scripting practice and improves our code readability as & denotes that we are executing native legacy commands inside PowerShell.

For example, here is a sample PowerShell script:

Running the BAT file Using Invoke-Expression Command

It is also possible to run a batch file or script using the Invoke-Expression cmdlet, which allows you to run a string as if it were a command. We can do this if we want to script everything with the PowerShell flavor instead of using the & operator. For example, to run a batch file located in the C:\Scripts directory, you can use the following command:

We can also use the Invoke-Expression cmdlet to run a command stored in a variable. For example:

Running the BAT file by Calling the Command Prompt

In PowerShell, like any other terminal, we can call an executable file. Also, it is good that Command Prompt has a dedicated executable file called cmd.exe. We can use this executable file to run the BAT file for us inside the PowerShell environment.

We only have to add a parameter /c to the cmd.exe file and append the path to the BAT file. The /c parameter is similar to the previous Invoke-Expression command, which converts the string value to a command inside the executable file.

Running the BAT file Using the Start-Process Command

This command is similar to the previous two introduced ways for the last method. We can use the Start-Process command to start a process (usually an executable file) and pass in arguments if needed. This command is much better than the last method, as we can keep the PowerShell writing style.

We append the Start-Process command with two parameters, -FilePath and -NoNewWindow. The file path parameter determines the BAT file location we will execute, and the -NoNewWindow parameter starts the defined process in the current window without opening another PowerShell window.

Alternatively, we can transform the previous command to accept arguments using the -ArgumentList parameter.

As we can see, I also added a new parameter called -Verb with a value of RunAs that will allow PowerShell to start the process in Administrator mode.

We can simplify this further by just providing the values as a shorthand form. So now our command will look similar to our previous commands but written in PowerShell scripting style.

The first value signifies which process to start, and the second value is the file path together with arguments (if there are any).

These are examples of running batch files or scripts inside the PowerShell environment. Depending on our specific needs, we may need different approaches to run our scripts and batch files.

That’s all about how to run bat file in PowerShell

Was this post helpful?

Leave a Reply

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