◐ Shell
clean mode source ↗

Handle null reference exception in CsvCommands.cs: ConvertPSObjectToCSV by mikkas456 · Pull Request #26144 · PowerShell/PowerShell

@mikkas456

PR Summary

This PR handles a null reference exception in the method ConvertPSObjectToCSV in the file CsvCommands.cs. This fixes a crash that would occur when passing a hash table with a null value to Export-Csv and ConvertTo-Csv. This PR also adds several test cases to Export-Csv.Tests.ps1 and ConvertTo-Csv.Tests.ps1, which will test if Export-Csv and ConvertTo-Csv can properly handle hash tables.

PR Context

Closes #25836

PR Checklist

@mikkas456

@microsoft-github-policy-service agree

iSazonov

Comment on lines +1047 to +1055

//Handle null reference exception for existing property with null value
if (dictionary[propertyName] == null)
{
value = null;
}
else
{
value = dictionary[propertyName].ToString();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify the code?

//Handle null reference exception for existing property with null value
if (dictionary[propertyName] == null)
{
value = null;
}
else
{
value = dictionary[propertyName].ToString();
}
value = dictionary[propertyName]?.ToString();

Also please remove obvious comment.

Comment on lines +240 to +243

It 'should convert hashtable with null values without error'{
{ $TestHashTable | ConvertTo-Csv } | Should -Not -Throw
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the test since next one does the check.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I have addressed these issues in another commit.

@iSazonov iSazonov added the CL-General

Indicates that a PR should be marked as a general cmdlet change in the Change Log

label

Oct 3, 2025

@iSazonov

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

iSazonov

xtqqczze

if (dictionary.Contains(propertyName))
{
value = dictionary[propertyName].ToString();
value = dictionary[propertyName]?.ToString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if value is null here, we should try GetToStringValueForProperty?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you know how to reproduce a scenario where this could be happen please open new issue.

@iSazonov

TravisEz13 pushed a commit to TravisEz13/PowerShell that referenced this pull request

Nov 5, 2025

SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request

Dec 12, 2025

kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request

Jan 2, 2026

@TravisEz13

@PowerShell/powershell-maintainers I don't think we can bring this to 7.6 and therefore not 7.5. removing the backport label unless we get more feedback.

JustinGrote pushed a commit to JustinGrote/PowerShell that referenced this pull request

Jun 2, 2026