Running the code $? = $true
in powershell crashes the program with a "powershell has stopped working" windows error message.
Some builtins like $true
or $host
throw an error: Cannot overwrite variable true because it is read-only or constant.
. Others like $myinvocation
or $null
are just ignored. Why is $?
different? Is there documentation on which builtins have which response? help about_automatic_variables
doesn't have any details.
I'm currently running version 5.1. I also tried using powershell -version $n '$? = $true'
and versions 3-5 all crash. Interestingly, versions 1 and 2 do not; they throw the read-only variable error.
It looks like this is a known bug. The fix was added to the version that is currently in works (6.0). You can find related pull request here.
My guts tell me that the reason for different behaviour (when compared to other automatic variable) is the fact that both you and system tries to write to this variable virtually at the same time. And even though I don't know why you would want to write to it (I suspect it all started with some
if ($? = $true)
...), I do agree that one is really nasty.