From StackOverflow
$inputFile = Get-Content "C:\path\to\data.txt"
$outputFile = "C:\path\to\dataoutput.txt"
$collate = foreach($Obj in $inputFile) {
$begin = ""
$end = ","
$begin + $Obj + $end
}
Set-Content -path $outputFile -value $collate
Just copy paste the commands in notepad and save the file as a ps1 file. Example : add_comma_to_every_line_in_a_file.ps1
Run with powershell : PS > .\add_comma_to_every_line_in_a_file.ps1
Orange
Apple
Pineapple
Orange,
Apple,
Pineapple,
This script can be very helpul to add characters at the beginning and at the end of every line too. You only need to specify what you want at the $begin
and at the $end
Example:
$inputFile = Get-Content "C:\path\to\data.txt"
$outputFile = "C:\path\to\dataoutput.txt"
$collate = foreach($Obj in $inputFile) {
$begin = "'"
$end = "',"
$begin + $Obj + $end
}
Set-Content -path $outputFile -value $collate
Orange
Apple
Pineapple
'Orange',
'Apple',
'Pineapple',