Skip to content

Export-Csv: -Append should be mutually exclusive with -NoHeader #25820

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

  • -NoHeader is invariably implied when using -Append, assuming that the target file exists and contains data, which only succeeds if the existing data starts with a header row.

  • However, if the target file doesn't exist yet or is an empty file, -Append -NoHeader creates the file without a header row, and that causes subsequent calls targeting the same with the same switches to fail.

$tempFile = New-TemporaryFile

@(
[pscustomobject] @{ prop = 'A' },
[pscustomobject] @{ prop = 'B' }
) | 
  ForEach-Object {
    $_ | Export-Csv -Append -NoHeader $tempFile
  } 

$tempFile | Remove-Item

Expected behavior

A parameter-binding error that indicates that -Append and -NoHeader are mutually exclusive.

Actual behavior

Cannot append CSV content to the following file: <file>. 
The appended object does not have a property that corresponds to the following column: A.
To continue with mismatched properties, add the -Force parameter, and then retry the command.

That is, the combination of -Append and -NoHeader was accepted, and created a no-header CSV file in the first call, causing the second one to predictably fail.


If backward compatibility is a concern, the alternative is to selectively and quietly ignore -NoHeader if combined with -Append, in case the target file happens to either not yet exist or happens to be empty (0 bytes).

Error details

Environment data

PowerShell 7.6.0-preview.4

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