Skip to content

Commit b7e2913

Browse files
authored
Add -NoHeader parameter to ConvertTo-Csv and Export-Csv cmdlets (PowerShell#19108)
1 parent 462f825 commit b7e2913

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public abstract class BaseCsvWritingCommand : PSCmdlet
7272
[Alias("UQ")]
7373
public QuoteKind UseQuotes { get; set; } = QuoteKind.Always;
7474

75+
/// <summary>
76+
/// Gets or sets property that writes csv file with no headers.
77+
/// </summary>
78+
[Parameter]
79+
public SwitchParameter NoHeader { get; set; }
80+
7581
#endregion Command Line Parameters
7682

7783
/// <summary>
@@ -301,7 +307,7 @@ protected override void ProcessRecord()
301307
}
302308

303309
// write headers (row1: typename + row2: column names)
304-
if (!_isActuallyAppending)
310+
if (!_isActuallyAppending && !NoHeader.IsPresent)
305311
{
306312
if (NoTypeInformation == false)
307313
{
@@ -727,16 +733,20 @@ protected override void ProcessRecord()
727733
if (_propertyNames == null)
728734
{
729735
_propertyNames = ExportCsvHelper.BuildPropertyNames(InputObject, _propertyNames);
730-
if (NoTypeInformation == false)
731-
{
732-
WriteCsvLine(ExportCsvHelper.GetTypeString(InputObject));
733-
}
734736

735-
// Write property information
736-
string properties = _helper.ConvertPropertyNamesCSV(_propertyNames);
737-
if (!properties.Equals(string.Empty))
737+
if (!NoHeader.IsPresent)
738738
{
739-
WriteCsvLine(properties);
739+
if (NoTypeInformation == false)
740+
{
741+
WriteCsvLine(ExportCsvHelper.GetTypeString(InputObject));
742+
}
743+
744+
// Write property information
745+
string properties = _helper.ConvertPropertyNamesCSV(_propertyNames);
746+
if (!properties.Equals(string.Empty))
747+
{
748+
WriteCsvLine(properties);
749+
}
740750
}
741751
}
742752

test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ Describe "ConvertTo-Csv" -Tags "CI" {
9191
$result | Should -Not -Match ([regex]::Escape('#TYPE'))
9292
}
9393

94+
It "Does not include headers with -NoHeader" {
95+
$result = $testObject | ConvertTo-Csv -NoHeader
96+
$result | Should -BeExactly '"Hello","World"'
97+
}
98+
9499
It "Does not support -UseQuotes and -QuoteFields at the same time" {
95100
{ $testObject | ConvertTo-Csv -UseQuotes Always -QuoteFields "TestFieldName" } |
96101
Should -Throw -ErrorId "CannotSpecifyQuoteFieldsAndUseQuotes,Microsoft.PowerShell.Commands.ConvertToCsvCommand"

test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ Describe "Export-Csv" -Tags "CI" {
6868
$results[0] | Should -Not -Match ([regex]::Escape("#TYPE"))
6969
}
7070

71+
It "Does not include headers with -NoHeader when exported and can imported with headers" {
72+
$P1 | Export-Csv -Path $testCsv -NoHeader
73+
$results = Get-Content -Path $testCsv
74+
$results | Should -BeExactly '"first"'
75+
$results = Import-Csv -Path $testCsv -Header "P1"
76+
$results[0].P1 | Should -BeExactly "first"
77+
}
78+
79+
It "Does not include headers when imported with headers and exported using -NoHeader" {
80+
$P1 | Export-Csv -Path $testCsv
81+
(Import-Csv -Path $testCsv) | Export-Csv -Path $testCsv -NoHeader
82+
$results = Get-Content -Path $testCsv
83+
$results | Should -BeExactly '"first"'
84+
}
85+
7186
It "Includes type information when -IncludeTypeInformation is supplied" {
7287
$testObject | Export-Csv -Path $testCsv -IncludeTypeInformation
7388
$results = Get-Content -Path $testCsv

0 commit comments

Comments
 (0)