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
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
Actual behavior
Error details
Environment data
Visuals
No response