title: Powershell - add leading zeros in CSV File
body: <p>I built a powershell script in order to format the content of a csv file. It is working fine for the majority of columns except for one column named &quot;Kunde_PLZ&quot;</p>
  <p>I want to add leading zeros to the content of that column &quot;Kunde_PLZ&quot; if the number inside the column is shorter than 4 digits. Trying to use padLeft(5,&quot;0&quot;) for that.</p>
  <pre><code>Import-Csv &quot;c:\temp1.csv&quot; -Delimiter &quot;;&quot; | 
  % {$_.BESTELLEINGANG = ([datetime]($_.BESTELLEINGANG)).ToString('dd.MM.yyyy');$_.LIEFERDATUM = ([datetime]($_.LIEFERDATUM)).ToString('dd.MM.yyyy');
  $_.KUNDENNUMMER =($_.KUNDENNUMMER) -replace '\.', '';$_.KUNDENNUMMER =($_.KUNDENNUMMER) -replace ',.*', '';$_.KUNDE_PLZ  = $_.KUNDE_PLZ.padLeft(5,&quot;0&quot;)   ;$_} | 
  sort Kundennummer -Descending | Select * -ExcludeProperty &quot;KUNDE_ZUSATZ&quot; | 
  Export-Csv &quot;c:\resultfile.csv&quot; -Delimiter &quot;;&quot; -Encoding UTF8 -notypeinfo -Force
  </code></pre>
  <p>But no leading zeros are added in the resultfile for the column &quot;KUNDE_PLZ&quot;.</p>
  <p>I also tried using foreach , but still no leading zeros added</p>
  <pre><code>$file = Import-Csv &quot;C:\temp1.csv&quot; -Delimiter &quot;;&quot; | 
  % {$_.BESTELLEINGANG = ([datetime]($_.BESTELLEINGANG)).ToString('dd.MM.yyyy');$_.LIEFERDATUM = ([datetime]($_.LIEFERDATUM)).ToString('dd.MM.yyyy');
  $_.KUNDENNUMMER =($_.KUNDENNUMMER) -replace '\.', '';$_.KUNDENNUMMER =($_.KUNDENNUMMER) -replace ',.*', '';$_} | 
  sort Kundennummer -Descending | Select * -ExcludeProperty &quot;KUNDE_ZUSATZ&quot;
  
  $file | foreach{$_.KUNDE_PLZ  = $_.KUNDE_PLZ.padLeft(5,&quot;0&quot;) }
  $file | Export-Csv &quot;C:\resultfile.csv&quot; -Delimiter &quot;;&quot; -Encoding UTF8 -notypeinfo -Force
  </code></pre>
  <p>Any ideas how to get the leading zeros added to the column &quot;Kunde_PLZ&quot; inside the csv file ?</p>