Skip to content

Instantly share code, notes, and snippets.

@mcc85s
Last active November 10, 2022 13:51
Show Gist options
  • Save mcc85s/9c32506a5e6f4e09b08b1bf50f02e3c2 to your computer and use it in GitHub Desktop.
Save mcc85s/9c32506a5e6f4e09b08b1bf50f02e3c2 to your computer and use it in GitHub Desktop.
An idea to extend process execution/handling
$Path = "File path"
$Arguments = "Arguments"
If (![System.IO.File]::Exists($Process))
{
Throw "File does not exist"
}
$Name = Split-Path $Path -Leaf
$WorkingDirectory = Split-Path $Path -Parent
$Process = [System.Diagnostics.Process]::New()
$ProcessName = $ProcessPath |
$Process.StartInfo = [System.Diagnostics.ProcessStartInfo]::New($Name)
$Process.WorkingDirectory = $WorkingDirectory
$Process.StartInfo.RedirectStandardOutput = 1
$Process.StartInfo.UseShellExecute = 0
$Process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
$Process.Start()
$Process.BeginOutputReadLine()
$Process.WaitForExit()
$exitCode = $Process.ExitCode
$Process.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment