Start-Process

Start one or more processes, optionally as a specific user.

Syntax
      Start-Process [-FilePath] string [[-ArgumentList] string[]]
         [-Credential PSCredential] [-LoadUserProfile]
            [-NoNewWindow] [-PassThru] [-RedirectStandardError FileNameString]
               [-RedirectStandardInput FileNameString] [-RedirectStandardOutput FileNameString]
                  [-UseNewEnvironment] [-Wait] [-WindowStyle ProcessWindowStyle]
                     [-WorkingDirectory string] [CommonParameters]

      Start-Process [-FilePath] string [[-ArgumentList] string[]]
         [-PassThru] [-Verb string] [-Wait]
            [-WindowStyle  {Normal | Hidden | Minimized | Maximized}]
               [-WorkingDirectory DirectoryString] [CommonParameters]

Key
   -ArgumentList string[]
       Specify parameters or parameter values to use when starting the process.
       The parameter name (-ArgumentList) is optional.

   -Credential PSCredential
       A user account that has permission to perform this action.
       Type a user-name, such as "User64" or "Domain64\User64", or enter a
       PSCredential object, such as one from the Get-Credential cmdlet.
       By default, the cmdlet uses the credentials of the current user.

   -FilePath string
       The path (optional) and file name of the program that runs in the
       process. Enter the name of an executable file or of a document,
       such as a .txt or .doc file, that is associated with a program.
       Required.

       If only a file name is given, use -WorkingDirectory to specify the path.

   -LoadUserProfile
       Load the Windows user profile stored in the HKEY_USERS registry key
       for the current user. The default value is FALSE.

       This parameter does not affect the PowerShell profiles. (See about_Profiles.)

   -NoNewWindow
       Prevent the process from running in a new window.
       By default, the process runs in a new window.
       You cannot use the -NoNewWindow and -WindowStyle parameters in the same command.

   -PassThru
       Return a process object for each process that the cmdlet started.
       By default, this cmdlet does not generate any output.

   -RedirectStandardError FileNameString
       Send any errors generated by the process to a file that you specify.
       Enter the path and file name. By default, the errors are displayed in the console.

   -RedirectStandardInput FileNameString
       Read input from the specified file.
       Enter the path and file name of the input file.
       By default, the process gets its input from the keyboard.

   -RedirectStandardOutput FileNameString
       Sends the output generated by the process to a file.
       Enter the path and file name. 
       By default, the output is displayed in the console.

   -UseNewEnvironment
       Use new environment variables specified for the process.
       By default, the started process runs with the environment variables
       specified for the computer and user.

   -Verb string
       A verb to be used when starting the process, such as Edit, Open, or Print.
       Each File type has a set of verbs that you can use.
       Some common verbs are: 
            .cmd  Edit, Open, Print, Runas
            .exe  Open, RunAs 
            .txt  Open, Print, PrintTo
            .wav  Open, Play 

       Runas will run the new process with elevated (admin) privileges and this
       will open in a separate window.
       To find the verbs that can be used with the process, use the Verbs property of the object.

   -Wait
       Wait for the specified process to complete before accepting more input.
       This parameter suppresses the command  prompt or retains the window
       until the process completes.

   -WindowStyle ProcessWindowStyle
       The state of the windows used for the process.
       Valid values are Normal, Hidden, Minimized, and Maximized.

   -WorkingDirectory DirectoryString
       The file directory that holds the executable file or document that runs
       in the process.  The default is the current directory.

Start-Process is equivalent to the the .NET System.Diagnostics.Process method.

When Start-Process is used to run a command line executable or script the output will show in a separate console window. To run a command script in the same window, use & (call).

When running an external Windows .EXE executable from a PowerShell script, by default the script will not wait and will immediately continue to the next command. Running the command through, Start-Process -wait program.exe will force PowerShell to wait for the process to exit before continuing.
An alternative method of doing this is Piping the command to out-null or out-default

Standard Aliases for Start-Process: saps, start

Examples

Start a notepad process and open Test.txt in the current directory:

PS C:\> start-process notepad.exe Test.txt

Start a notepad process and open Test.txt in the current directory (using the Static Start method):

PS C:\> [Diagnostics.Process]::Start("notepad.exe","test.txt")

Run a batch file with elevated (Admin) privileges:

PS C:\> start-process -filepath C:\batch\demo.cmd -verb runas

Start a process that prints the C:\Demo\MyFile.txt file:

PS C:\> start-process myfile.txt -workingdirectory "C:\Demo" -verb Print

Start the Notepad process. Maximizes the window and wait until the process completes:

PS C:\> start-process notepad -wait -windowstyle Maximized

Start a process that sorts a file and returns the sorted items in Sorted.txt , write any errors to SortError.txt :

PS C:\> start-process Sort.exe -RedirectStandardInput C:\Demo\Testsort.txt -RedirectStandardOutput C:\Demo\Sorted.txt -RedirectStandardError C:\Demo\SortError.txt

Start a process using the WMI.Win32_Process class’s Create() function and return the Process ID, this PID can then be used later to alter or stop the process:

PS C:\> $pclass = [wmiclass]'root\cimv2:Win32_Process'
PS C:\> $new_pid = $pclass.Create('notepad.exe', '.', $null).ProcessId

Create function syntax: .Create(commandline, currentdirectory, parameters)

Start a process using the .NET System.Diagnostics.Process class’s start method and return the Process ID:

PS C:\> $new_pid = [diagnostics.process]::start("notepad.exe")

Start a process using the -passthru parameter to return a process object:

PS C:\> $proc = Start-Process notepad.exe -Passthru

PS C:\> $proc.ID
9504

PS C:\> Get-Process -id $proc.id
 Handles  NPM(K)    PM(K)     WS(K)     CPU(s)      Id  SI ProcessName
 -------  ------    -----      -----     ------     --  -- -----------
     234      13     3056      12752       0.08   9504   1 notepad

PS C:\> Stop-Process -id 9504

“Somebody should tell us, right at the start of our lives, that we are dying. Then we might live life to the limit, every minute of every day. Do it! I say. Whatever you want to do, do it now! There are only so many tomorrows” ~ Pope Paul VI

Related PowerShell Cmdlets

--% - Stop parsing input.
Get-Command - Retrieve basic information about a command.
Invoke-Command - Run commands on local and remote computers.
Invoke-Expression - Run a PowerShell expression.
Invoke-Item - Invoke an executable or open a file (START).
PowerShell.exe - Launch a PowerShell session/run a script.
Get-Process - Get a list of processes on a machine.
Stop-Process - Stop a running process (Kill).
. (source) - Run a command script in the current shell (persist variables and functions).
& (call) - Run a command script.
Equivalent CMD command: Start (internal command)


 
Copyright © 1999-2024 SS64.com
Some rights reserved