Powershell: modifying text – Education Career Blog

ok, I give up! why doesn’t this work? Im just trying to loop through a csv file and replace any value in the nth column with some value.

$source = "C:\blah.csv"

(gc $source) | foreach{ $_.Split(',')10 = 'something'} | sc $source


Basically, what you are trying is something like that:

$s = 'a,b,c,d,e'
$s.Split(',')4 = 'something'

That won’t work, because you don’t assign ‘something’ to any variable.

I would either read the file as csv (via Import-CSV) or (if it structure is really simple) use regexes:

$s = 'c,d,e', 'c,d,x', 'z,f,d'
$s | % { $_ -replace '(?<=(^,*,){2}).*','something'}

Leave a Comment