Import form one file, manipulate data and write to another file
<p>Using PowerShell a CSV file needs to be read, operations performed on some fields and only specific fields output to a new file.</p>

<p>How would this be accomplished in PS?</p>

<p>Just to keep this as simple and minimal as possible, the CSV is being written to a fixed length file, so in this example the PCode is being prefixed with "00".</p>

<p>The the real implementation the columns will be left padded with zeroes, spaces and then each column will trimmed to only be a specified number of characters wide.</p>

<p><code>InFile.csv</code>:</p>

<pre>ClientNumber,AccountNumber,PCode,Amount
c10,a000000001,p1,100.01
c11,a000000002,p2,200.02</pre>

<p><code>OutFile.csv</code>:</p>

<pre>ClientNumber,AccountNumber,PCode,Amount
c10a00000000100p1100.01
c11a0000000000p2200.02</pre>

<pre><code>$InFile  = "C:\InFile.csv"
$OutFile = "C:\OutFile.csv"

$InCSV = Import-Csv $InFile -Header "ClientNumber","AccountNumber","ZCode","Amount" |
         ConvertTo-Csv -NoTypeInformation  

foreach ($record in $CSV) {
    # left pad with zeroes
    $RR = "000" + $record.RRCode
    # write $RR to file
} 

#$CSV | Out-File $OutFile -Force -Encoding Ascii
</code></pre>