Problem
You want to launch a new process on the system, but also want to configure its startup environment.
Solution
To launch a new process, use the [System.Diagnostics.Process]::Start() method. To control its startup environment, supply it with a System.Diagnostics. ProcessStartInfo object that you prepare, as shown in Example 211.
Example 211. Configuring the startup environment of a new process
$credential = GetCredential
## Prepare the startup information (including username and password) $startInfo = NewObject Diagnostics.ProcessStartInfo $startInfo.UserName = $credential.Username $startInfo.Password = $credential.Password $startInfo.Filename = "powershell"
## Start the process $startInfo.UseShellExecute = $false [Diagnostics.Process]::Start($startInfo)
Discussion
Normally, launching a process in PowerShell is as simple as typing the program name:
PS >notepad c:\temp\test.txt
However, you may sometimes need detailed control over the process details, such as its credentials, startup directory, environment variables, and more. In those situations, use the [System.Diagnostics.Process]::Start() method to provide that functionality.
The following function acts like the cmd.exe start command and like the Start | Run dialog in Windows:
PS >function start { [Diagnostics.Process]::Start($args) }
PS >start www.msn.com