Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public abstract class BaseCsvWritingCommand : PSCmdlet
[Alias("UQ")]
public QuoteKind UseQuotes { get; set; } = QuoteKind.Always;

/// <summary>
/// Gets or sets property that writes csv file with no headers.
/// </summary>
[Parameter]
public SwitchParameter NoHeader { get; set; }

#endregion Command Line Parameters

/// <summary>
Expand Down Expand Up @@ -301,7 +307,7 @@ protected override void ProcessRecord()
}

// write headers (row1: typename + row2: column names)
if (!_isActuallyAppending)
if (!_isActuallyAppending && !NoHeader.IsPresent)
{
if (NoTypeInformation == false)
{
Expand Down Expand Up @@ -727,16 +733,20 @@ protected override void ProcessRecord()
if (_propertyNames == null)
{
_propertyNames = ExportCsvHelper.BuildPropertyNames(InputObject, _propertyNames);
if (NoTypeInformation == false)
{
WriteCsvLine(ExportCsvHelper.GetTypeString(InputObject));
}

// Write property information
string properties = _helper.ConvertPropertyNamesCSV(_propertyNames);
if (!properties.Equals(string.Empty))
if (!NoHeader.IsPresent)
{
WriteCsvLine(properties);
if (NoTypeInformation == false)
{
WriteCsvLine(ExportCsvHelper.GetTypeString(InputObject));
}

// Write property information
string properties = _helper.ConvertPropertyNamesCSV(_propertyNames);
if (!properties.Equals(string.Empty))
{
WriteCsvLine(properties);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ Describe "ConvertTo-Csv" -Tags "CI" {
$result | Should -Not -Match ([regex]::Escape('#TYPE'))
}

It "Does not include headers with -NoHeader" {
$result = $testObject | ConvertTo-Csv -NoHeader
$result | Should -BeExactly '"Hello","World"'
}

It "Does not support -UseQuotes and -QuoteFields at the same time" {
{ $testObject | ConvertTo-Csv -UseQuotes Always -QuoteFields "TestFieldName" } |
Should -Throw -ErrorId "CannotSpecifyQuoteFieldsAndUseQuotes,Microsoft.PowerShell.Commands.ConvertToCsvCommand"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ Describe "Export-Csv" -Tags "CI" {
$results[0] | Should -Not -Match ([regex]::Escape("#TYPE"))
}

It "Does not include headers with -NoHeader when exported and can imported with headers" {
$P1 | Export-Csv -Path $testCsv -NoHeader
$results = Get-Content -Path $testCsv
$results | Should -BeExactly '"first"'
$results = Import-Csv -Path $testCsv -Header "P1"
$results[0].P1 | Should -BeExactly "first"
}

It "Does not include headers when imported with headers and exported using -NoHeader" {
$P1 | Export-Csv -Path $testCsv
(Import-Csv -Path $testCsv) | Export-Csv -Path $testCsv -NoHeader
$results = Get-Content -Path $testCsv
$results | Should -BeExactly '"first"'
}

It "Includes type information when -IncludeTypeInformation is supplied" {
$testObject | Export-Csv -Path $testCsv -IncludeTypeInformation
$results = Get-Content -Path $testCsv
Expand Down