diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs
index a447a8983b..2434ad836f 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs
@@ -72,6 +72,12 @@ namespace Microsoft.PowerShell.Commands
[Alias("UQ")]
public QuoteKind UseQuotes { get; set; } = QuoteKind.Always;
+ ///
+ /// Gets or sets property that writes csv file with no headers.
+ ///
+ [Parameter]
+ public SwitchParameter NoHeader { get; set; }
+
#endregion Command Line Parameters
///
@@ -301,7 +307,7 @@ namespace Microsoft.PowerShell.Commands
}
// write headers (row1: typename + row2: column names)
- if (!_isActuallyAppending)
+ if (!_isActuallyAppending && !NoHeader.IsPresent)
{
if (NoTypeInformation == false)
{
@@ -727,16 +733,20 @@ namespace Microsoft.PowerShell.Commands
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);
+ }
}
}
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1
index 129da4a9fd..0d48426081 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1
@@ -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"
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1
index 962576f824..67234f24c5 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1
@@ -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