Start-Process
is a powershell commandlet used create new process. As powershell is mainly developed .Net and provides .Net feature Start-Process
is equivalent of System.Diagnostics.Process
method.
Start A Process
The most basic usage of the Start-Process
is start a process by giving its name. Start-Process inherits current working environment variables and can find given executable if its in Path
environment. In this example we will start notepad.exe
by specifying a filename names.txt
PS> Start-Process notepad.exe names.txt
Run Batch File
We can specify a script file or batch file to run in Powershell. We will use -filepath
option with the full path and name of batch file. In this example we run batch file backup.cmd
.
PS> Start-Process -filepath C:\backup.cmd
Run With Elevated Admin Privileges
While running and creating new processes with Start-Process the current user privilege will be get. This may not work some situations. We can elevate privileges to the Administrator with -verb runas
. In this example we will run cmd.exe
with Administrator privileges.
PS> Start-Process -verb runas cmd.exe
Set Working Directory
While working with Start-Process
by default current working directory will be current working directory. Setting working directory of given process can be changed with -workingdirectory
option. This is useful if we need to change executable file path. In this example we will change to the C:\Windows
to the working directory. Providing directories in double quotes will make this less error prone.
PS> Start-Process -workingdirectory "C:\Windows" cmd.exe
Use Print Verb
We can also use Start-Process
without providing any executable file. We can use print verb to print text files. We will use -verb Print
for this. In this example we will print the file named names.txt
PS> Start-Process names.txt -verb Print
Set WindowStyle
While starting the new process it will open new windows in default size. We can change this window size on startup. We can use -windowstyle
option. This option can get following values
- Maximized
- Minimized
In this example we will open notepad.exe
in maximized window.
PS> Start-Process notepad.exe -windowstyle Maximized
Redirect Standard Input
Commands and executables running in a shell will have some standard input. Standard input provided data will be fed into command or executable. We can use -RedirectStandardInput
option with the file which contains data to be fed into executable. In this example we will put data in mydata.txt
into notepad.exe
.
PS> Start-Process notepad.exe -RedirectStandardInput mydata.txt
Redirect Standard Input
As done in previous example we can redirect standard output into given file. We will use -RedirectStandardOuput
with a file name. In this example we will redirect into file named notepad.log
.
PS> Start-Process notepad.exe -RedirectStandardOutput notepad.log