Editing CSV columns query

Hi all,

So I’ve had a a request to edit a csv file by replacing column values with a a set of unique numbers. Below is a sample of the original input file with a a header line followed by a couple of rows. Note that the rows have NO column headers.

HH ### SLH304 01100001 2 20200606 20200605 011100

What I’m looking to do is change say the values in column 3 with a unique number.

So far I have the following …

$txtin = Get-Content "C:\Temp\InFile.txt" | ForEach {"$($_.split('|'))"-replace $_[2],$i++} |Out-File C:\Temp\csvout.txt

… but this isn’t working as it removes the delimiter and adds numbers in the wrong places …

1D1D1 1G1Z1F1D1 1I1E1 1U1S1D1 12101210101610151 1E1U1R1210101711171 1 1
2D2D2 2G2Z2F2D2 2I2E2 2U2S2D2 22202220202620252 2E2U2R2220202721272 2 2

Any ideas on how to resolve would be much appreciated.

Rob, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

If your CSV file is a valid CSV file you should treat it as such and use Import-Csv instead of Get-Content and Export-Csv instead of Out-File. Please read the help completely including the examples to learn how to use it.