Introduction to PowerShell Start-Process
Many times when we are working with process and file system then we need some way by which we could be able to handle files opening and writing and executing to any .exe file from command. Start-Process command allows us to open process at by command line. It has the power to capture error or output of the command in any mentioned file or read inputs from any mentioned file. Other than simply opening any process it allows many more functionalities like, We can use Start-Process to define some alternative ways to
- Getting or loading user(it will take current user profile details if nothing passed) profile details,
- Allow to start a new process in various formats(normal, minimized, maximized, etc) in the new Window. In Simple manner, it defines state for the window for a new process
- An alternative option for credentials. If we do not mention credential section then it will read current user credentials
Syntax and Parameters
Syntax of PowerShell Start-Process are given below:
Syntax #1:
Start-Process
[-FilePath] <String>
[[-ArgumentList] <defines string parameters value>]
[-Credential <related to permission for the user to run the command, it can be PSCredential
also>]
[-WorkingDirectory <string value of working directories >]
[-LoadUserProfile<Here it will load user profile>]
[-NoNewWindow]
[-PassThru<Holds the details of process like process id etc>]
[-RedirectStandardError <string path of file where error will be written>]
[-RedirectStandardInput <string path of file from where input will be read>]
[-RedirectStandardOutput <string path of file to where output will be written>]
[-WindowStyle <Allow us to open in various formats like normal ,maximized and minimized>]
[-Wait<wait for previous input processing>]
[-UseNewEnvironment]
[-WhatIf<display what happen on execution of command>]
[-Confirm<display a confirmation before execution of command>]
[<CommonParameters>]
Syntax #2:
Start-Process
[-FilePath] <String>
[[-ArgumentList] <defines parameters value>]
[-WorkingDirectory <String>]
[-PassThru]
[-Verb <String>]
[-WindowStyle <ProcessWindowStyle>]
[-Wait<wait for previous input processing>]
[-WhatIf<display what happen on execution of command>]
[-Confirm<display a confirmation before execution of command>]
[<CommonParameters>]
Parameters
Parameters of PowerShell Start-Process.
- ArgumentList: It defines parameter values that will be used when the command will start. We can use space also between parameters, the only thing we need to take care of when space is taken in that case we should put parameters in escaped double quotes.
- Confirm: Its name is clearly specifying its meaning. It will prompt confirmation before you run the command for ensuring from your side.
- Credential: It’s used for security purposes, It defines a user with permission to run the command. If we do not pass parameters for this command it will take credentials of the current users.
- FilePath: Defines the alternative path and filename for the program which will run in the process. Here we need to pass the directory along with the file name(file for processing), if we do not provide path or directory it will use the current working directory with the same file name.
- LoadUserProfile: It will load the profile of the current Windows user which is stored in the HKEY_USERS registry.
- NoNewWindow: It allows us to run a new process in the current Windows console. Remember PowerShell will always open a new window if we do not provide value to this parameter.
- RedirectStandardError: It defines a file. With the help of this command, we can send errors generated by our command to a file Here we need to mention the file name for capturing this error. So if we do not mention the file then it will send error on the console.
- RedirectStandardInput: Suppose you want our process to read inputs from any file then we can use this parameter. In this parameter, we have to mention the path of the file from where our command will read inputs. If we do not mention file name then the process will read inputs from entering values from the keyboard.
- RedirectStandardOutput: Again, here if we want to capture output generated by the process to any file then we can define file name and path, and the output of the process will be posted on the mentioned file. In case if we do not mention file name than output will be displayed on the console screen.
- UseNewEnvironment: This command takes any new environment variables that defines the process. In case if we do not pass any argument to this command it will take environment variables of current computer and user.
- Wait: Suppose you have passed many inputs to process in command, in that case, we use this attribute(Wait) as it will allow the process to wait to complete processing of previous input.
- WhatIf: It will display consequences after executing the command. This command will support only after PowerShell version 6 or later.
- WindowStyle: It will allow us to open a new process in various formats according to our convenience on the computer. It has several formats like Normal, Hidden, Minimize, and maximized. We need this command because it gives us various ways to open the process and see them.
Examples to Implement PowerShell Start-Process
Following are the powershell start-process are:
Example #1
In the below example we are inside the ranjan1 folder and we want to open a file with name test2.txt in any default editor. It started the process by opening the file. It is a simple example where we are just opening a given file, please see the example along with the screen.
Start-Process test2.txt
Output:
It will open the text file specified.
Example #2
This is the based example where we can see the creation of any process and stopping of that process with process id. Here we are capturing process details with the help of command called -Passthru which generally gets current process details with object formats. In this example variable $procDetails contains details of the process which started. We can get the process id from this variable by writing $procDetails.id and with the id we can perform any operation on the current running process. For example below we are stopping the process with the unique process id. And the qwfile will be closed .
$procDetails = Start-Process test2.txt -Passthru
$procDetails.id
Output:
Stop-Process -id 9820
Output:
It will close the text file according to id which is specified above.
Example #3
In this example we are using –Confirm, we can see on the execution of below command first it is asking for yes(Y) or not (N), so if we enter Y than it will open the file, and if we Enter N it would not open the file. Please follow the below command along with the screen.
Start-Process -FilePath "test2.txt" --Confirm
Recommended Articles
This is a guide to PowerShell Start-Process. Here we also discuss the introduction and syntax of powershell start-process along with different examples and its code implementation. You may also have a look at the following articles to learn more –