Starting a Process in PowerShell

PowerShell supports multiple methods for starting a process with the most common being the Start-Process cmdlet. Others include the Call Operator (&) or using Dot Sourcing. First lets take a look at the Start-Process cmdlet.

Start-Process


Start-Process is a native PowerShell cmdlet with the sole purpose of starting a process on a local machine. It supports passing arguments, specifying credentials, and setting the working directory among other options. The following is an example of opening FireFox to a specific URL:

                                        
  Start-Process "C:\Mozilla Firefox\firefox.exe" -ArgumentList "www.msdn.com"

                                        

Note that you can use the "-Wait" switch if you want PowerShell to wait for the process to exit. You can also use the -RunAs switch to start a process as an Administrator:

                                        
  #
  # Open an admin session of PowerShell ISE
  #
  Start-Process PowerShell_ISE -Verb RunAs -Wait

                                        


Call Operator (&)


The "Call operator" or "Invocation Operator" is commonly used to run a command, script, or script block. It can be used to start a process and also allows for some powerful capabilities such as running a command that is defined in a variable. See the below examples where a PowerShell command is stored in a variable and then invoked via the Call Operator:

                                        
  #
  # Start Notepad and open Text.txt
  #
  PS C:\Windows> & "C:\Windows\Notepad" Test.txt

  #
  # Create a variable with it's contents being a command
  #
  PS C:\Windows> $cmd = "Get-Process -Name *svchost*"
  
  # Invoke the command
  PS C:\Windows> & $cmd

   Id ProcessName
   -- -----------
 3260 SMSvcHost
 3336 SMSvcHost
  564 svchost
  776 svchost

                                        


Dot Sourcing


Dot Sourcing is typically used to call another PowerShell script if you need to add functions or variables into your session. However, you can also use Dot Sourcing to start a process as shown below:

                                        
  # Open Paint
  . "C:\Windows\System32\mspaint.exe"

  # Call a PowerShell script that adds functions
  . "C:\Scripts\CommonFunctions.ps1"

                                        

In summary, while these approaches do overlap some in functionality it's important to be aware of the differences of each.

Bonus Tip:

  If you are interested in starting a process on a remote machine
  take a look at PsExec

                                        


© 2019 Embrs.net