Fairly simple request but struggling. I have a .csv file with 30 columns. One column 'sku' has product codes in it which I need to modify. I then need to write all the data including the modified column, back to the original file.
So far I have selected and expanded the 'sku' object and used some regex to do the modifications, this works as expected.
However if I export the file using Export-Csv I only get the text property value .length. I suspect because -replace is implicitly converting the object to a string?
Also I'm not selecting any of the other columns as I can't figure out how to select all the other properties and do the -replace together.
Please help me out.
$CSVFiles = Get-ChildItem -Path D:\Exports\Test\AMAZON -File -Filter '*.csv' | Where Extension -eq '.csv'
foreach ($csv in $CSVFiles) {
$csvOutput = Import-Csv $csv.FullName
$sku = $csvOutput | Select-Object -ExpandProperty sku |
Foreach-Object {$_ -replace '^.*?(?=T)',''} |
Export-Csv $csv.FullName -NoType
}
With Select-Object -ExpandProperty
you turn rich and powerfull objects with properties into stupid text. ;-)
This should be closer to what you need:
$CSVFiles =
Get-ChildItem -Path D:\Exports\Test\AMAZON -File -Filter '*.csv'
foreach ($csv in $CSVFiles) {
$csvOutput = Import-Csv $csv.FullName
$csvOutput |
Foreach-Object {
$_.sku = $_.sku -replace '^.*?(?=T)'
$_
} |
Export-Csv -Path $csv.FullName -NoTypeInformation -Force
}