Prerequisites
There is asymmetry between the serialization of properties with either AutomationNull ([Management.Automation.Internal.AutomationNull]::Value) or $null values, that affects the following formats:
This issue focuses on CSV. I will address (CLI)XML in a separate issue as the behavior differs.
Consider the following contrived example:
[pscustomobject] @{
Prop = if ($false) { 'Foo' } else { $null }
} | ConvertTo-Csv
# "Prop"
[pscustomobject] @{
# Statement assignments have pipeline semantics, so Prop receives AutomationNull.
Prop = if ($false) { 'Foo' }
} | ConvertTo-Csv
# "Prop"
# ""
A typical PS user who is unaware of AutomationNull (and arguably should not have to be) would reasonably expect equivalent CSV serialization. Yet, the result differs because AutomationNull as a property value serializes as a quoted ("") empty field whereas $null serializes as unquoted.
Notes:
Steps to reproduce
[pscustomobject] @{ P1 = & {}; P2 = $null } | ConvertTo-Csv
Expected behavior
CSV serialization of AutomationNull and $null property values should be identical.
The result should be either:
# "P1","P2"
# "",""
# Or...
# "P1","P2"
# ,
Depending on whether AutomationNull/$null should both be a quoted or unquoted empty field. Note that this has deserialization implications, as noted in the aforementioned #17702 issue.
Actual behavior
Serialization of AutomationNull and $null property values differs.
AutomationNull: "" (quoted)
$null: Nothing (unquoted)
Environment data
Name Value
---- -----
PSVersion 7.5.0-rc.1
PSEdition Core
GitCommitId 7.5.0-rc.1
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Prerequisites
There is asymmetry between the serialization of properties with either
AutomationNull([Management.Automation.Internal.AutomationNull]::Value) or$nullvalues, that affects the following formats:This issue focuses on CSV. I will address (CLI)XML in a separate issue as the behavior differs.
Consider the following contrived example:
A typical PS user who is unaware of
AutomationNull(and arguably should not have to be) would reasonably expect equivalent CSV serialization. Yet, the result differs becauseAutomationNullas a property value serializes as a quoted ("") empty field whereas$nullserializes as unquoted.Notes:
ConvertFrom-Csvis inconsistently$Null#17702, this issue is especially problematic when the resultant CSV data is deserialized withConvertFrom-Csv/Import-Csv.ConvertTo-Jsonin Windows PowerShell, but has since been addressed. In the latest PS version,AutomationNulland$nullboth serialize asnullin JSON.Steps to reproduce
Expected behavior
CSV serialization of
AutomationNulland$nullproperty values should be identical.The result should be either:
Depending on whether
AutomationNull/$nullshould both be a quoted or unquoted empty field. Note that this has deserialization implications, as noted in the aforementioned #17702 issue.Actual behavior
Serialization of
AutomationNulland$nullproperty values differs.AutomationNull:""(quoted)$null: Nothing (unquoted)Environment data