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
Prerequisites
Steps to reproduce
-NoHeaderis 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 -NoHeadercreates the file without a header row, and that causes subsequent calls targeting the same with the same switches to fail.Expected behavior
A parameter-binding error that indicates that
-Appendand-NoHeaderare mutually exclusive.Actual behavior
That is, the combination of
-Appendand-NoHeaderwas 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
-NoHeaderif combined with-Append, in case the target file happens to either not yet exist or happens to be empty (0bytes).Error details
Environment data
Visuals
No response