Make Export-Csv -Append and -NoHeader mutually exclusive (#26472)

This commit is contained in:
Yoshifumi
2025-11-22 12:38:35 +05:00
committed by GitHub
parent f4476c5226
commit 8948c417c0
3 changed files with 15 additions and 0 deletions
@@ -270,6 +270,14 @@ namespace Microsoft.PowerShell.Commands
this.ThrowTerminatingError(errorRecord);
}
// Validate that Append and NoHeader are not specified together.
if (Append && NoHeader)
{
InvalidOperationException exception = new(CsvCommandStrings.CannotSpecifyAppendAndNoHeader);
ErrorRecord errorRecord = new(exception, "CannotSpecifyBothAppendAndNoHeader", ErrorCategory.InvalidData, null);
this.ThrowTerminatingError(errorRecord);
}
_shouldProcess = ShouldProcess(Path);
if (!_shouldProcess)
{
@@ -157,4 +157,7 @@
<data name="EOFIsReached" xml:space="preserve">
<value>EOF is reached.</value>
</data>
<data name="CannotSpecifyAppendAndNoHeader" xml:space="preserve">
<value>You must specify either the -Append or -NoHeader parameters, but not both.</value>
</data>
</root>
@@ -187,6 +187,10 @@ Describe "Export-Csv" -Tags "CI" {
$results[1].PSObject.properties.Name | Should -Not -Contain 'third'
}
It "Should throw when -Append and -NoHeader are specified together" {
{ $P1 | Export-Csv -Path $testCsv -Append -NoHeader -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyBothAppendAndNoHeader,Microsoft.PowerShell.Commands.ExportCsvCommand"
}
It "First line should be #TYPE if -IncludeTypeInformation used and pstypenames object property is empty" {
$object = [PSCustomObject]@{first = 1}
$pstypenames = $object.pstypenames | ForEach-Object -Process {$_}