2

Is it possible to import a csv file into powershell, replace the entire header row with a defined set of data then save to csv file out with a new name.

I need to be able to automate this task so it can be used by another software.

Any ideas will be greatly appreciated.

2
  • Does it have to be PowerShell? It is certainly possible in PowerShell but might be even easier using Excel Macros or a scripting language like Perl or Python. Commented Jul 17, 2014 at 1:54
  • No, anything that can be completely automated can work. Just needs to be called by a scheduled task on a win 2012 server. Commented Jul 18, 2014 at 1:28

2 Answers 2

5

Sample file contents

1,b,4
8,j,7
8,k,9

To make your own header:

Import-Csv D:\Temp\txt.txt -Header "name","letter","value"

name                                                     letter                                                   value                                                  
----                                                     ------                                                   -----                                                  
1                                                        b                                                        4                                                      
8                                                        j                                                        7                                                      
8                                                        k                                                        9     

To skip rows:

Import-Csv D:\Temp\txt.txt -Header "name","letter","value" | select -skip 1

name                                                     letter                                                   value                                                  
----                                                     ------                                                   -----                                                  
8                                                        j                                                        7                                                      
8                                                        k                                                        9  

To tie it all up with an export:

$tempCSV = Import-Csv D:\Temp\txt.txt -Header "name","letter","value" | select -skip 1
$tempCSV | Export-CSV D:\Temp\txt.txt -NoTypeInformation
Sign up to request clarification or add additional context in comments.

2 Comments

This line wont work for me $tempCSV | Export-CSV Export-Csv C:\CSVTest\txt.txt -NoTypeInformation
I get this error Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "C:\CSVTest\txt.txt" to type "System.Char". Error: "String must be exactly one character long." At C:\CSVTest\Example.ps1:3 char:34 + $tempCSV | Export-CSV Export-Csv C:\CSVTest\txt.txt -NoTypeInformation + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand
1

A simple way to handle this can be: $temp = Get-Content ~\<CSVFileName>.csv; $temp[0] = $temp[0].replace($temp[0], "<NewCommaSeperatedHeader>"); Set-Content -Path ~\<ModHeaderCSVFile>.csv -Value $temp;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.