It's not a trivial problem. It's an easily avoidable one, though! :-)
This code assumes that the string has a multiple of columns and that there isn't any "odd" rows in the string. The one condition I'm not sure whether it's accounted for is empty columns (e.g., something like "1,,3,4"). Nor does the code expect to find commas in a column's data!
$d = "data1,data2,data3,data4,data5,data6,data7,data8,data9,data10,data11,data12"
# a hash of column names
$row = [ordered]@{
A = ""
B = ""
C = ""
D = ""
}
# get just the names of the columns in a row
[array]$cols = $row.Keys
$lastchar = $d.length
$startsearch = 0
While ($startsearch -lt $lastchar){
0..($cols.length - 1)|
ForEach-Object{
$pos = $d.Substring($startsearch,$lastchar).IndexOf(",") # zero based
$row[$cols[$_]] = $d.Substring($startsearch,$pos)
$startsearch = $startsearch + $pos + 1
$lastchar = $lastchar - $pos -1
}
[PSCustomObject]$row
} Export-CSV SomeFileName.csv -NoTypeInformation