Usefulness of the last command result new behavior in PowerShell 7

In case you missed it, there is a change in the behavior of the Last Command Result variable $? with PowerShell 7.
Here is the excerpt of the official About Automatic Variables document:

Until PowerShell 7, containing a statement within parentheses (...),
subexpression syntax $(...) or array expression @(...) always reset $? to True,
so that (Write-Error) shows $? as True. This has been changed in PowerShell 7,
so that $? will always reflect the actual success of the last command run
in these expressions.

Meaning

Until now, the Last Command Result variable $? did not always accurately reflect the result of the former command.

However, now when you use PowerShell 7 it does!

Example of code

Statements

With PowerShell 5.1

With PowerShell 7

Subexpressions

With PowerShell 5.1

With PowerShell 7

Array subexpressions

With PowerShell 5.1

With PowerShell 7

Usefulness

This new behavior can be very useful to detect the difference between a truly empty value from a value being empty because of an error.

Here is an example, in which the computer could not be contacted
and the same piece of code is executed on both PowerShell 5.1 and PowerShell 7.

With PowerShell 5.1 the Last Command Result variable $? variable is useless
and leads to conclude that there is not drive Y on the remote computer.

Conversely, with PowerShell 7, the Last Command Result variable $? leads to conclude that something wrong happened while the cmdlet tried to collect the data on the remote computer.

Caveats

If your statement, subexpression, or array subexpression is used by another PowerShell piece of code, you will still get the result of the latest one, like you did with PowerShell 5.1.

For example:

Why do we get True instead of False?

  1. Though the subexpression computing the date throws an error, it returns nothing (i.e. $null) to the Write-Output cmdlet.
  2. For the Write-Output cmdlet, $null is not an error, it’s just a particular type of value. Therefore, the Write-Output cmdlet is able to process the request normally.
  3. The Last Command Result variable $?, which is the result of the Write-Output cmdlet, is true.

Impact of the change

This change cannot really be considered as a “breaking” change. Why?

Until now when you used the Last Command Result variable $? all you could expect was to “not detect” an error.
As a consequence, you had to do more verifications after checking the Last Command Result variable $?.
Therefore it is really unlikely that this change would oblige you to update your existing code.

Furthermore, most of the people just didn’t use the Last Command Result variable $? because of this know issue.

However, now with PowerShell 7, you can start using the Last Command Result variable $? again and you will not necessarily need more verification after checking its content. In many cases, this can even simplify your code.

2 thoughts on “Usefulness of the last command result new behavior in PowerShell 7

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s