A Better PowerShell Start Process

We can start a process in PowerShell many different ways. We’ve got the PowerShell Start Process and Invoke-Expression cmdlets, we can call the executable directly or use the ampersand (&) to invoke expressions. The most common way is to use Start-Process because it’s probably the most intuitive. PowerShell is known for it’s intuitive approach to command naming and Start-Process is an excellent choice. However, that command is limited.

To understand why Start-Process and all of these other commands are limited you first have to understand how a typical EXE file works. When an EXE file is run, it performs whatever action it was designed to do; it pings something (ping), starts a software installer (setup), looks up some DNS record (nslookup), whatever. For this part, Start-Proces and other commands to start a process work great. It’s simple. The limitation comes when that EXE returns some output.

Streams

An EXE file like many other executable files not limited to Windows has a concept of standard streams. Standard streams are how executable files return output. These streams come in three flavors; stdin, stdout, and stderr. Stdin is the stream that can get passed into the executable which we won’t be focusing on here. Stdout is the stream that the executable uses to send normal non-error output. In PowerShell terms, think of stdout as what Write-Output returns. When an error occurs (depending on if the developer of the executable wrote it correctly), the executable should return output via stderr. This is a stream that’s set aside for returning any error information.

These streams allow users of these executable files to differentiate between what’s typical output and what’s an error. Streams have been around for decades and Windows thus PowerShell know all about them and have adopted their own.

Exit Codes

An exit code, return code or result code is another age-old concept that executable files follow as well. An exit code is an integer that allows the executable to signal a status to the user when it exits.

There are some standard exit codes that programs are supposed to follow like if the exit code is 0, everything is fine, exit code 3010 means it needs a reboot and so on. PowerShell can capture this exit code a few different ways like using the $LastExitCode automatic variable. Exit codes are another method that the executable file communicates to the user.

Capturing Streams and Exit Codes

Now that you understand what we’re working with let’s use an example. To keep it simple, I’ll use the good ol’ ping.exe. First, I’ll ping google.com which will return a successful result. We aren’t using PowerShell start process here.

I’ll now purposefully ping a host that doesn’t resolve a DNS name and will fail. Notice the value of $LastExitCode.

You saw that the exit code differs because the result of ping was different but you didn’t see streams because natively PowerShell doesn’t understand the difference when it comes to executable files. The text you see in the console is white; not the red error text you know and love.

We can capture the streams and redirect to files a few different ways like using > and 2> like below. This is the old school way.

Notice though that, in this case, even though ping.exe is returning an exit code of 1 (as shown above), that output is still going to stdout. This is common. What stream and exit code that’s returned is completely up to the developer and comes in a lot of different styles, unfortunately.
If you’d like to go the "new school" way you could use Start-Process and use the -RedirectStandardError and -RedirectStandardOutput parameters, but they’d still just go to files.

Limitations of Start-Process

You can see that starting a process and returning some common output isn’t too common. On top of that, PowerShell doesn’t handle the streams and exit codes too well. If I were to use Start-Process to run ping.exe and want to track what happened in the result I’d have to do something like this every time I wanted to run an executable file.

I still wouldn’t get my error text either!

Let’s fix all of this. Download a small function I created called Invoke-Process from the PowerShell Gallery.

Now run ping using a valid host and see what happens.

start-process

Notice how we get the same output. This is good!

Now run ping using an invalid host.

PowerShell error

Now notice that we get the typical red error text as we’d expect if something went wrong. Invoke-Process uses Start-Process under the covers to capture stdout, stderr and the exit code and then sends the output to whatever stream it’s supposed to go to. This is how it should be!
Too bad PowerShell didn’t make it easier to work with streams and exit codes from executable files, but I suppose nowadays, we can add that in there now that it’s open source!

Leave a Reply