Share via


How to append header upto four columns using powershell in csv file

Question

Friday, August 26, 2016 5:02 AM

Hi,

I am novice at powershell scripting and i want to write a script in which I can append Name, FilePath, Specification, DateTime as column headers and export that csv file.

Please anyone can help me out for the same.

Thanks in advance.

All replies (6)

Friday, August 26, 2016 2:22 PM ✅Answered

Users CSV File
Name,SamAccountName
User1,User1
User2,User2
User3,User3

In order to append new header, you need to type the existing header as well.

Import-Csv D:\Tests\Users.csv -Header Name,SamAccounName,First,Last

Output
Name,SamAccountName,First,Last
User1,User1
User2,User2
User3,User3


Friday, August 26, 2016 3:43 PM ✅Answered

Quick generalized method"

Import-CsvD:\Tests\Users.csv | Select *, Name, FilePath, Specification, DateTime

This avoids the extra bad line caused by header replacement.

\(ツ)_/


Friday, August 26, 2016 5:11 AM

Hello,

Please use the below PS commands to generate the headers - 

$filedata = import-csv $file -Header Name , FilePath , Specification  , DateTime  
$filedata | export-csv $file -NoTypeInformation

Thanks

Kailas

Please Vote or Mark as an answer if found useful


Friday, August 26, 2016 5:19 AM

https://technet.microsoft.com/en-us/dd742419

\(ツ)_/


Friday, August 26, 2016 10:34 AM | 1 vote

Hi, Please try this

import-csv .\file1.csv | select Name, FilePath, Specification, DateTime | export-csv .\file2.csv -noTypeInformation

Please make sure to vote and mark as answer if you find this really useful.

Regards

Kam


Monday, August 29, 2016 5:33 AM

Hi JayK,

You may also try this:

$prompts = @{'name'='123';'FilePath'='c:\share';'Specification'='None';'DateTime'=$(Get-Date)}
$obj = New-Object -TypeName psobject -Property $prompts
$outs = Write-Output $obj
$outs | Export-Csv -Path C:\share\1234.csv   -NoTypeInformation 

Best regards,

Andy_Pan

Please remember to mark the replies as an answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected].