You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
$null should be indicated by a blank line, especially when within an array #17683
Consequently, the result of the script accidentally is an array of [$null, 1], but:
PS C:\<PATH> .\ResultWithAccidentalNull.ps1
1 <= seems to look good
But when assigning the result to a variable it gets queerish:
PS C:\<PATH> $result = .\ResultWithAccidentalNull.ps1
PS C:\<PATH> $result
1 <= still seems good
PS C:\<PATH> $result.Length
2 <= what ?!?
PS C:\<PATH> $result[0] <= nothing ?!?
PS C:\<PATH> $result[1]
1 <= only now it becomes apparent where the intended result went to
This behavior is rather confusing:
The $null value exists but is not indicated in any way.
This behavior is also inconsistent:
A result[1] of an array with 2 or more items is expected to be indicated on the second line.
The real world script of course is much bigger. Can you imagine how long it took us to find the root cause of this issue? I think there would be an obvious fix to improve this situation.
Ideal result of this script:
PS C:\<PATH> $result = .\ResultWithAccidentalNull.ps1
PS C:\<PATH> $result
<= result[0] is $null
1 <= result[1] is 1
$null when written to the host needs to be converted to a string, so it becomes an empty string:
($null|out-string) -eq""
This is fine, no need to change anything on this behavior.
However, I think it is inconsistent behavior when outputting an array containing $null. Why? If "$null" -eq "", outputting an array containing "$null"or"" should result in the same. But it doesn't. Array values of "" result in empty lines, whereas array values of "$null" result in nothing.
This doesn't make sense. The fact that lines are broken, i.e. an array value per line is output, is the behavior of outputting an array. It should have nothing to do with the value of the respective array item. The value of an array item should only define the content of the line, but not the fact that a line is output.
Of course I do understand the compatibility / breaking change related considerations. Still, I think such inconsistency in behavior should also be considered to get fixed in the long run, in order to prevent people falling into (unecessary) traps.
Yeah, it can be quite confusing to track down null values as it is, and this behaviour makes it even more confusing at times.
I'd even be open to us writing out $null or NULL or something to make them easier to spot, but a blank entry instead of skipping the entry would be a good step forward nonetheless.
This issue has been marked as by-design and has not had any activity for 1 day. It has been closed for housekeeping purposes.
Hmm... msftbot... Closing after just a day of inactivity? What if there is inactivity over a weekend? What if the by-design tag was applied a bit too early and no real resolution is there yet? Besides PowerShell handling $null values within an array, MS should also consider refining the configuration of the msftbot...
If the request is purely changing how it is formatted, then formatting changes are not considered breaking changes (from a scripting point of view anyways). Since $true is represented as True, then it may make sense for $null to be represented as NULL.
Reacted by Rain Sallow (/u/ta11ow) and Steve Brown
Tangentially related but possibly also potentially warranting a specific display usecase would be [dbnull]::Value which is currently indistinguishable in console output from $null.
Hi maettu-this - thank you for reporting this issue. The Interactive WG has reviewed this feature request. As mentioned previously in this issue, the suggested change would create substantial breaking changes. The working group recommends a work around using foreach to iterate the values if needed.
Activity
SteveL-MSFT commentedon Jul 18, 2022
$nullwhen written to the host needs to be converted to a string, so it becomes an empty string:Replacing it with a newline would certainly be a breaking change and can cause confusion in the other direction.
In your example,
$result[0]returns$nullwhich is an empty string so nothing is written out. You can check that$null -eq $result[0]is$true.Since
$nullis a valid value in an array, some folks are likely depending on this behavior.JamesWTruher commentedon Jul 18, 2022
it's also not necessary to
($null | out-string) -eq ""as"$null" -eq ""gives the same resultmaettu-this commentedon Jul 19, 2022
This is fine, no need to change anything on this behavior.
However, I think it is inconsistent behavior when outputting an array containing
$null. Why? If"$null" -eq "", outputting an array containing"$null"or""should result in the same. But it doesn't. Array values of""result in empty lines, whereas array values of"$null"result in nothing.This doesn't make sense. The fact that lines are broken, i.e. an array value per line is output, is the behavior of outputting an array. It should have nothing to do with the value of the respective array item. The value of an array item should only define the content of the line, but not the fact that a line is output.
Of course I do understand the compatibility / breaking change related considerations. Still, I think such inconsistency in behavior should also be considered to get fixed in the long run, in order to prevent people falling into (unecessary) traps.
alxfnd commentedon Jul 19, 2022
Why don't you try assigning the values into a hash table and reading from the table?
$result = .\ResultWithAccidentalNull.ps1
$table = @{
A = $result[0]
B = $result[1]
}
$table
This should print out your null value and makes it easier to reference specific values later on.
SeeminglyScience commentedon Jul 19, 2022
I believe they're saying that formatting shouldn't skip
$null, but instead treat it the same way it does an empty string:Not weighing in on if it should be done, just clarifying the request.
vexx32 commentedon Jul 19, 2022
Yeah, it can be quite confusing to track down null values as it is, and this behaviour makes it even more confusing at times.
I'd even be open to us writing out
$nullorNULLor something to make them easier to spot, but a blank entry instead of skipping the entry would be a good step forward nonetheless.maettu-this commentedon Jul 22, 2022
Hmm... msftbot... Closing after just a day of inactivity? What if there is inactivity over a weekend? What if the
by-designtag was applied a bit too early and no real resolution is there yet? Besides PowerShell handling$nullvalues within an array, MS should also consider refining the configuration of the msftbot...maettu-this commentedon Jul 22, 2022
Even more so that apparently I am not the only to question the handling of
$null, see comments and #17701.SteveL-MSFT commentedon Aug 1, 2022
If the request is purely changing how it is formatted, then formatting changes are not considered breaking changes (from a scripting point of view anyways). Since
$trueis represented asTrue, then it may make sense for$nullto be represented asNULL.vexx32 commentedon Aug 1, 2022
Tangentially related but possibly also potentially warranting a specific display usecase would be
[dbnull]::Valuewhich is currently indistinguishable in console output from$null.JamesWTruher commentedon Oct 30, 2023
this would be a fairly substantial breaking change if addressed
theJasonHelmick commentedon Nov 1, 2023
Hi maettu-this - thank you for reporting this issue. The Interactive WG has reviewed this feature request. As mentioned previously in this issue, the suggested change would create substantial breaking changes. The working group recommends a work around using foreach to iterate the values if needed.
microsoft-github-policy-service commentedon Nov 2, 2023
This issue has been marked as declined and has not had any activity for 1 day. It has been closed for housekeeping purposes.