Skip to content

Asymmetry between CSV serialization of properties with AutomationNull or $null values #24699

Closed

Description

@surfingoldelephant

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:

  • 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:

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)
"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

  1. added
    Needs-Triage
    on Dec 22, 2024
  2. added
    WG-NeedsReview
    WG-Cmdlets
    on Dec 23, 2024
  3. weaslevdmd commented on Jan 7, 2025

    @weaslevdmd

    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.

  4. iRon7 commented on Jan 20, 2025

    @iRon7

    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 $Null but not an empty string. See e.g. this use case:

    ($Env:Path | ConvertFrom-Csv -Delimiter ';' -Header (0..99)).PSObject.Properties.Value
  5. mklement0 commented on Jan 30, 2025

    @mklement0
    Contributor

    iRon7, as previously discussed, member-access enumeration does include $null values, namely if the property exists, but happens to contain $null

    # -> $true
    ('foo;bar' | ConvertFrom-Csv -Delimiter ';' -Header (1..3)).PSObject.Properties.Value -contains $null

    A conceptually clearer example:

    class Foo { [object] $Bar }
    
    $o1 = [Foo]::new() # .Bar is $null
    $o2 = [Foo]::new(); $o2.Bar = 42
    
    # -> $true, because the member-access enumeration resulted in @($null, 42)
    ($o1, $o2).Bar -contains $null 

    Member-access enumeration only omits $null values if the property doesn't exist on a given input object:

    # -> $false; because $PSVersionTable doesn't have a .Year property, it didn't contribute to the output.
    ((Get-Date), $PSVersionTable).Year -contains $null
  6. added
    Resolution-Declined
    WG-Reviewed
    and removed
    WG-NeedsReview
    Needs-Triage
    on Feb 5, 2025
  7. SteveL-MSFT commented on Feb 5, 2025

    @SteveL-MSFT
    Member

    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.

  8. microsoft-github-policy-service commented on Feb 7, 2025

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as declined and has not had any activity for 1 day. It has been closed for housekeeping purposes.

  9. microsoft-github-policy-service commented on Feb 7, 2025

    @microsoft-github-policy-service
    Contributor

    📣 Hey surfingoldelephant, how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-DeclinedWG-CmdletsWG-Reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions