Start-Process

Starts one or more processes on the local computer.

语法

Default (默认值)

Start-Process
    [-FilePath] <String>
    [[-ArgumentList] <String[]>]
    [-Credential <PSCredential>]
    [-WorkingDirectory <String>]
    [-LoadUserProfile]
    [-NoNewWindow]
    [-PassThru]
    [-RedirectStandardError <String>]
    [-RedirectStandardInput <String>]
    [-RedirectStandardOutput <String>]
    [-WindowStyle <ProcessWindowStyle>]
    [-Wait]
    [-UseNewEnvironment]
    [<CommonParameters>]

UseShellExecute

Start-Process
    [-FilePath] <String>
    [[-ArgumentList] <String[]>]
    [-WorkingDirectory <String>]
    [-PassThru]
    [-Verb <String>]
    [-WindowStyle <ProcessWindowStyle>]
    [-Wait]
    [<CommonParameters>]

说明

The Start-Process cmdlet starts one or more processes on the local computer. To specify the program that runs in the process, enter an executable file or script file, or a file that can be opened by using a program on the computer. If you specify a non-executable file, Start-Process starts the program that is associated with the file, similar to the Invoke-Item cmdlet.

You can use the parameters of Start-Process to specify options, such as loading a user profile, starting the process in a new window, or using alternate credentials.

示例

Example 1: Start a process that uses default values

This example starts a process that uses the Sort.exe file in the current folder. The command uses all of the default values, including the default window style, working folder, and credentials.

Start-Process -FilePath "sort.exe"

Example 2: Print a text file

This example starts a process that prints the C:\PS-Test\MyFile.txt file.

Start-Process -FilePath "myfile.txt" -WorkingDirectory "C:\PS-Test" -Verb Print

Example 3: Start a process to sort items to a new file

This example starts a process that sorts items in the Testsort.txt file and returns the sorted items in the Sorted.txt files. Any errors are written to the SortError.txt file.

Start-Process -FilePath "Sort.exe" -RedirectStandardInput "Testsort.txt" -RedirectStandardOutput "Sorted.txt" -RedirectStandardError "SortError.txt" -UseNewEnvironment

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

Example 4: Start a process in a maximized window

This example starts the Notepad.exe process. It maximizes the window and retains the window until the process completes.

Start-Process -FilePath "notepad" -Wait -WindowStyle Maximized

Example 5: Start PowerShell as an administrator

This example starts PowerShell by using the "Run as administrator" option.

Start-Process -FilePath "powershell" -Verb RunAs

Example 6: Using different verbs to start a process

This example shows how to find the verbs that can be used when starting a process. The available verbs are determined by the filename extension of the file that runs in the process.

$startExe = New-Object System.Diagnostics.ProcessStartInfo -Args PowerShell.exe
$startExe.verbs
open
runas

The example uses New-Object to create a System.Diagnostics.ProcessStartInfo object for PowerShell.exe, the file that runs in the PowerShell process. The Verbs property of the ProcessStartInfo object shows that you can use the Open and RunAs verbs with PowerShell.exe, or with any process that runs a .exe file.

Example 7: Specifying arguments to the process

Both commands start the Windows command interpreter, issuing a dir command on the 'Program Files' folder. Because this foldername contains a space, the value needs surrounded with escaped quotes. Note that the first command specifies a string as ArgumentList. The second command a string array.

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

参数

-ArgumentList

Specifies parameters or parameter values to use when this cmdlet starts the process. If parameters or parameter values contain a space, they need surrounded with escaped double quotes.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False
别名:Args

参数集

(All)
Position:1
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Credential

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

参数属性

类型:PSCredential
默认值:None
支持通配符:False
不显示:False
别名:RunAs

参数集

Default
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-FilePath

Specifies the optional path and filename 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 on the computer. This parameter is required.

If you specify only a filename, use the WorkingDirectory parameter to specify the path.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:PSPath

参数集

(All)
Position:0
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-LoadUserProfile

Indicates that this cmdlet loads the Windows user profile stored in the HKEY_USERS registry key for the current user.

This parameter does not affect the PowerShell profiles. For more information, see about_Profiles.

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False
别名:Lup

参数集

Default
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-NoNewWindow

Start the new process in the current console window. By default PowerShell opens a new window.

You cannot use the NoNewWindow and WindowStyle parameters in the same command.

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False
别名:nnw

参数集

Default
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-PassThru

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

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-RedirectStandardError

Specifies a file. This cmdlet sends any errors generated by the process to a file that you specify. Enter the path and filename. By default, the errors are displayed in the console.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:RSE

参数集

Default
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-RedirectStandardInput

Specifies a file. This cmdlet reads input from the specified file. Enter the path and filename of the input file. By default, the process gets its input from the keyboard.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:RSI

参数集

Default
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-RedirectStandardOutput

Specifies a file. This cmdlet sends the output generated by the process to a file that you specify. Enter the path and filename. By default, the output is displayed in the console.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:RSO

参数集

Default
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-UseNewEnvironment

Indicates that this cmdlet uses new environment variables specified for the process. By default, the started process runs with the environment variables specified for the computer and user.

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False

参数集

Default
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Verb

Specifies a verb to use when this cmdlet starts the process. The verbs that are available are determined by the filename extension of the file that runs in the process.

The following table shows the verbs for some common process file types.

File type Verbs
.cmd Edit, Open, Print, RunAs, RunAsUser
.exe Open, RunAs, RunAsUser
.txt Open, Print, PrintTo
.wav Open, Play

To find the verbs that can be used with the file that runs in a process, use the New-Object cmdlet to create a System.Diagnostics.ProcessStartInfo object for the file. The available verbs are in the Verbs property of the ProcessStartInfo object. For details, see the examples.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

UseShellExecute
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Wait

Indicates that this cmdlet waits for the specified process and its descendants to complete before accepting more input. This parameter suppresses the command prompt or retains the window until the processes finish.

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-WindowStyle

Specifies the state of the window that is used for the new process. The acceptable values for this parameter are: Normal, Hidden, Minimized, and Maximized. The default value is Normal.

You cannot use the WindowStyle and NoNewWindow parameters in the same command.

参数属性

类型:ProcessWindowStyle
默认值:None
接受的值:Normal, Hidden, Minimized, Maximized
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-WorkingDirectory

Specifies the location of the executable file or document that runs in the process. The default is the folder for the new process.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

输入

None

You cannot pipe input to this cmdlet.

输出

None, System.Diagnostics.Process

This cmdlet generates a System.Diagnostics.Process object, if you specify the PassThru parameter. Otherwise, this cmdlet does not return any output.

备注

  • This cmdlet is implemented by using the Start method of the System.Diagnostics.Process class. For more information about this method, see Process.Start Method.