Repository navigation
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
Activity
weaslevdmd commentedon Jan 7, 2025
My use case may be slightly different, but we ingest a ton of malformed CSV data from various vendors and government agencies. We are not in control of these data but are responsible for ingesting said data into a SQL database - typically using OPENROWSET with BULK load operations (which requires a .fmt format file to be defined prior to loading).
In our automation, we use powershell to 'clean-up' much of the malformed data so that it works (better) with BULK loads using the following command:
Import-Csv $InFilePath | Export-Csv $OutFilePath -NoTypeInformation -Encoding UTF8
This does an amazing job of removing issues related to inconsistent use of quotes, etc. - which allows us some assurances the .FMT files will work as expected.
However, due to the issue being discussed in this thread, we always have to check the final character in the output file and if it matches the expected delimiter we have to add an additional "" to the file. Without this empty field being added we get BULK load errors on the SQL server side. I would love to remove this extra check if Export-Csv just 'worked' as needed.
I would vote to have an option to force the output to include the final NULL column to the appropriate AutomationNull value (double-quoted by default). By making it an optional override, you don't have to break existing functionality unless overridden.
iRon7 commentedon Jan 20, 2025
Be aware that any improvements towards a more consisted result, might result in a break change as e.g. PowerShell member access enumeration feature omits
$Nullbut not an empty string. See e.g. this use case:mklement0 commentedon Jan 30, 2025
iRon7, as previously discussed, member-access enumeration does include
$nullvalues, namely if the property exists, but happens to contain$nullA conceptually clearer example:
Member-access enumeration only omits
$nullvalues if the property doesn't exist on a given input object:[pscustomobject]s: see Member enumeration malfunctions with array that contains 2 or more [pscustomobject] instances, [pscustomobject] contributes$nullto the result even for non-existent properties #13752SteveL-MSFT commentedon Feb 5, 2025
The @PowerShell/wg-powershell-cmdlets discussed this. We do think a change in behavior is a breaking change and given the high use of the csv cmdlets, we anticipate there would be some folks who depend on the current behavior. We also agree that users of the conversion cmdlets should validate their input to ensure they get the right output they are expecting.
microsoft-github-policy-service commentedon Feb 7, 2025
This issue has been marked as declined and has not had any activity for 1 day. It has been closed for housekeeping purposes.
microsoft-github-policy-service commentedon Feb 7, 2025
📣 Hey surfingoldelephant, how did we do? We would love to hear your feedback with the link below! 🗣️
🔗 https://aka.ms/PSRepoFeedback