Skip to content

Export-Csv cannot export hashtables with null values #25836

Description

@exchange12rocks

Prerequisites

Steps to reproduce

I noticed that Export-Csv cannot export hashtables with properties that have null values, with an error "Object reference not set to an instance of an object".

Expected behavior

PS> @{'a'=$null} | Export-Csv .\test.csv; Get-Content .\test.csv
"a"

Actual behavior

PS> @{'a'=$null} | Export-Csv .\test.csv
Export-Csv: Object reference not set to an instance of an object.

Error details

Exception             :
    Type       : System.NullReferenceException
    TargetSite :
        Name          : ConvertPSObjectToCSV
        DeclaringType : [Microsoft.PowerShell.Commands.ExportCsvHelper]
        MemberType    : Method
        Module        : Microsoft.PowerShell.Commands.Utility.dll
    Message    : Object reference not set to an instance of an object.
    Source     : Microsoft.PowerShell.Commands.Utility
    HResult    : -2147467261
    StackTrace :
   at Microsoft.PowerShell.Commands.ExportCsvHelper.ConvertPSObjectToCSV(PSObject mshObject, IList`1 propertyNames)
   at Microsoft.PowerShell.Commands.ExportCsvCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
CategoryInfo          : NotSpecified: (:) [Export-Csv], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.ExportCsvCommand
InvocationInfo        :
    MyCommand        : Export-Csv
    ScriptLineNumber : 1
    OffsetInLine     : 16
    HistoryId        : 53
    Line             : @{'a'=$null} | Export-Csv .\test.csv
    Statement        : Export-Csv .\test.csv
    PositionMessage  : At line:1 char:16
                       + @{'a'=$null} | Export-Csv .\test.csv
                       +                ~~~~~~~~~~~~~~~~~~~
    InvocationName   : Export-Csv
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.2
PSEdition                      Core
GitCommitId                    7.5.2
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility moduleWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Reviewed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions