How to Start Processes With Windows PowerShell

  1. the Start-Process Cmdlet in PowerShell
  2. the Start-Process Cmdlet Parameters
  3. the Benefits of PowerShell Start-Process
How to Start Processes With Windows PowerShell

The Start-Process cmdlet is a PowerShell command used to start single or more processes in a controlled and managed way. By default, the started process inherits all current PowerShell environments.

The Start-Process cmdlet can execute or run an executable file, batch script, MS-DOS and PowerShell command, even Java application. In addition, Windows PowerShell can use the Start-Process cmdlet to specify user profile, windows status, and credentials, etc.

This article will discuss how the Start-Process cmdlet works and utilize it when writing our scripts.

the Start-Process Cmdlet in PowerShell

The Start-Process cmdlet executes one or more processes, executable or script files, or any files that an installed software can open on the computer.

The Start-Process cmdlet has a basic syntax shown below when using Windows PowerShell.

Start-Process <string>

the Start-Process Cmdlet Parameters

The Start-Process cmdlet can use parameters to add more power, functionality, and flexibility to the cmdlet.

Start New Process or Executable

As mentioned, the most basic usage of the Start-Process command is providing the executable file, batch or script file, or command like the below syntax. For example, a Notepad application will open when called with the following syntax.

Start-Process notepad.exe

Alternatively, the cmdlet can use the -FilePath parameter to specify the file location we want to execute.

Start-Process -FilePath notepad.exe

We can also specify the complete path of the executable file or batch file below. In the following example, we will execute a batch file located under the D:\scripts directory in the following example.

Start-Process -FilePath "D:\scripts\backup.bat"

Set Standard Input as File

We can specify a process input with the standard input where provided standard input content is redirected into the given process. In this case, the -RedirectStandardInput parameter can set a file as input into the newly created process.

Start-Process -FilePath "D:\scripts\backup.bat" -RedirectStandardInput test.txt

In the example syntax above, the backup.bat executable input comes from the file test.txt.

Set Standard Output as File

When a process is executed, it may create some output that we can print to the terminal, screen, or file. We can use the -RedirectStandardOutput parameter to specify the output into a file.

Start-Process -FilePath "D:\scripts\backup.bat" -RedirectStandardOutput test.txt

The backup.bat executable output comes from the file test.txt in the example syntax above.

Set Standard Error Output as File

While running a process, errors may occur, and information about these errors is printed into the console or terminal by default. Using the -RedirectStandardError parameter can redirect the output into a file like below.

Start-Process -FilePath "D:\scripts\backup.bat" -RedirectStandardError errors.txt

In the example syntax above, if we encountered any errors while running the backup.bat executable, the errors will be printed in the file errors.txt.

Set Working Directory

By default new process is executed in the current working directory, which is commonly the system drive C:. However, we can set a new working directory below using the -WorkingDirectory parameter.

Start-Process notepad.exe -WorkingDirectory "D:\"

For this example,

Create New Environment

Together with our previous parameters discussed in the article, we can merge them into one script block.

The -UseNewEnvironment parameter specifies that the process runs with its environment variables.

$processOptions = @{
    FilePath               = "sort.exe"
    RedirectStandardInput  = "TestSort.txt"
    RedirectStandardOutput = "Sorted.txt"
    RedirectStandardError  = "SortError.txt"
    UseNewEnvironment      = $true
}
Start-Process @processOptions

Start-Process in Maximized Window

The Start-Process command can start a command-line process or a GUI process that may have some GUI.

The script can set the GUI window size with the -WindowStyle parameter. This parameter can be set as Maximized to maximize the new process window.

Start-Process notepad.exe -WindowStyle Maximized

Start-Process With a Different User

By default, the started process is executed as the current user privileges. However, the Start-Process cmdlet can change the process’s privileges with the -Credential parameter by providing the new user with whom we want to execute the process.

If you type in your username, you will be prompted to enter a password.

Start-Process notepad.exe -Credential <username>

Start-Process as an Administrator

We can run the application as an administrator with the - Verb parameter.

Start-Process notepad.exe -Verb RunAs
Note
PowerShell may still need to ask for your confirmation due to your local computer’s User Account Control (UAC) despite running as administrator. We do not recommend entirely disabling UAC for security purposes.

Start-Process With Specified Arguments

Commands, processes, or batch files may accept single or multiple arguments to get input data.

This input data is called an argument, and the Start-Process command can provide arguments to the started process with the -ArgumentList. Provided argument list passed into the processes as arguments.

Start-Process -FilePath "$env:comspec" -ArgumentList "/c", "dir", "`"%systemdrive%\program files`""

the Benefits of PowerShell Start-Process

  • Script files only can be opened locally. It is a security technique that prevents remote attacks using Windows PowerShell scripts.
  • The cmdlet runs in a scripting environment that Microsoft supports. As long as Windows PowerShell is supported, Microsoft will dedicate resources to keep the language current, with update revisions.
  • A vast developer community readily shares knowledge specifically with the Start-Process cmdlet.
  • The cmdlets and system data stores use standard, consistent syntax and naming conventions to share data easily.
  • Using this cmdlet, the navigation of the operating system is simplified, which lets users familiarize the file system, the registry, and other data.
  • Objects can be easily manipulated directly or sent to other tools or databases.
  • Software vendors and developers can build custom tools.
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn