Export-CSV Differences

I’ve got a snippet of code that runs differently in PSVersion 5.1 and 7.4:

"server1.domain.com","server2.domain.com" | % { @{ ComputerName = $_; CopySucceeded = ( Test-Path "C:\temp\foo.bar" ) } } | Export-Csv C:\Temp\foo.csv -Append -Force -NoTypeInformation

When ran in 7.4 I get these results:
image

When ran in 5.1, I get this result:
image

Does anyone know what has changed to allow this to work now in 7.4 or which version began to allow this?

I’ve been trying to mess around in 5.1 to see how to export this, but not quite getting it with ConvertFrom-CSV. Is there a way to Export-CSV in 5.1 without declaring headers to get a similar output from 7.4?

"server1.domain.com","server2.domain.com" | ConvertFrom-Csv -Header ('ComputerName', 'CopySucceeded') | Export-Csv C:\Temp\foo5.1.csv -Append -Force -NoTypeInformation

I’m not sure if I really get what your question is but I wonder why you’re not using it like this:

"server1.domain.com", "server2.domain.com" | 
ForEach-Object { 
    [PSCustomObject]@{
        ComputerName   = $_; 
        CopySucceeded  = ( Test-Path 'C:\temp\foo.bar' ) 
    } 
} | 
Export-Csv .\foo$($PSVersionTable.PSVersion.Major).csv -Force -NoTypeInformation

This way you have in both versions the same result. :man_shrugging:t3:

1 Like

Yea this is probably the best move.

those extra properties are basically hash table properties.

$hashtable = @{}
$hashtable | Get-Member

Release v7.2.0-preview.9 Release of PowerShell · PowerShell/PowerShell · GitHub

ConvertTo-Csv / Export-Csv - Serialize IDictionary objects with key/value pairs as properties by vexx32 · Pull Request #11029 · PowerShell/PowerShell · GitHub

Make Export-Csv and ConvertTo-Csv support hashtables (dictionaries) · Issue #10999 · PowerShell/PowerShell · GitHub

basically its a serialization issue that was ‘fixed’ in version 7.2

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.