-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
There is asymmetry between the serialization of properties with either AutomationNull ([Management.Automation.Internal.AutomationNull]::Value) or $null values, that affects the following formats:
- CSV
- CLIXML
- XML
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:
- Due to The last cell of an empty column read by
ConvertFrom-Csvis inconsistently$Null#17702, this issue is especially problematic when the resultant CSV data is deserialized withConvertFrom-Csv/Import-Csv. - This issue is similar to a problem affecting JSON/
ConvertTo-Jsonin Windows PowerShell, but has since been addressed. In the latest PS version,AutomationNulland$nullboth serialize asnullin JSON.
Steps to reproduce
[pscustomobject] @{ P1 = & {}; P2 = $null } | ConvertTo-CsvExpected 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)
"P1","P2"
"",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