Skip to content

Instantly share code, notes, and snippets.

@marcoandre1
Last active December 4, 2022 22:55
Show Gist options
  • Save marcoandre1/3d5de6b973698997b451f599abdd3ad9 to your computer and use it in GitHub Desktop.
Save marcoandre1/3d5de6b973698997b451f599abdd3ad9 to your computer and use it in GitHub Desktop.
Add comma to every line in a file with powershell script

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',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment