# Format particulat column values of CSV file

**URL:** https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175
**Category:** PowerShell Help
**Created:** [March 3, 2017, 1:44am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175 "2017-03-03T01:44:30Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![rishabh-a](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@rishabh-a](https://forums.powershell.org/u/rishabh-a)
#### Post date: [March 3, 2017, 1:44am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/1 "2017-03-03T01:44:30Z")

</div>

I am having a CSV file with Column name as a,b,c,…ae,af.  
I have to format column b. It contains values like 20135 and i have to make it 13 digits by appending leading zero’s.  
e.g cell 1 of column b has value of 203500 after formatting it should be 0000000203500.  
I learned to format particular value like

```
$value = 203500
$formattedValue = "{0:D13}" -f $value
output is : 0000000203500
```

I learned it from some previous questions on this forum.But this is not working for me or i’am not doing it the correct way. What I am doing is-

```
$Imported = Import-Csv -Delimiter "," -Path C:\Users\risha\Desktop\PowerShell\testFile.csv
$columnName = $($Imported.b)
$Output = foreach ($i in $Imported) {
    foreach ($values in $columnName) {
        $formattedText = "{0:D13}" -f $values
        $values = $formattedText
    }
    $i
}
$Output
```

---

<div class="post-metadata">

### Author: ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)
#### Post date: [March 3, 2017, 1:56am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/2 "2017-03-03T01:56:42Z")

</div>

If you change

```
$formattedText = "{0:D13}" -f $values
```

to

```
$formattedText = "{0:D13}" -f [int] $values
```

it should work as expected because values you’ve imported are of the data type string not integer.

You can check out one of his recent post on a similar topic.

> **[Get-Content and Numbers](https://richardspowershellblog.wordpress.com/2017/03/01/get-content-and-numbers/)**
>
> A common technique is to put a list of information into a text file and read that using Get-Content. The information is often server names. This works great when the data is strings but breaks down…

---

<div class="post-metadata">

### Author: ![rishabh-a](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@rishabh-a](https://forums.powershell.org/u/rishabh-a)
#### Post date: [March 3, 2017, 2:11am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/3 "2017-03-03T02:11:24Z")

</div>

Thank you sir.

```
$formattedText = "{0:D13}" -f [int] $values
```

Gave appropriate value. but these values are not reflected in output.  
a : 100001338  
**b : 200000 –\> here it should show 0000000200000**  
c : 29  
d : 000002  
e : 600028008  
if i do

```
$columnName = $($Imported.b)
    foreach ($a in $columnName) {
        $formattedText = "{0:D13}" -f [int] $value
        $value = $formattedText
        Write-Host $value

    }
$Output
```

**$value shows the correct value that i want**  
but its not getting replaced in the column in output screen.

---

<div class="post-metadata">

### Author: ![rishabh-a](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@rishabh-a](https://forums.powershell.org/u/rishabh-a)
#### Post date: [March 3, 2017, 3:06am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/4 "2017-03-03T03:06:58Z")

</div>

Thank you Daniel Krebs.

```
$formattedText = "{0:D13}" -f [int] $values
```

Worked Fine. But my still there’s an issue.  
these values are not reflected in output.  
a : 100001338  
**b : 200000 –\> here it should show 0000000200000**  
c : 29  
d : 000002  
e : 600028008  
if i do

```
$columnName = $($Imported.b)
    foreach ($a in $columnName) {
        $formattedText = "{0:D13}" -f [int] $value
        $value = $formattedText
        Write-Host $value

    }
$Output
```

**$value** shows the correct value that i want but its not getting replaced in the column, at output screen.

---

<div class="post-metadata">

### Author: ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)
#### Post date: [March 3, 2017, 4:24am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/5 "2017-03-03T04:24:50Z")

</div>

In you last post I don’t see you updating the $Output variable.

Based on your original post the following should work.

```
$Imported = Import-Csv -Delimiter "," -Path C:\Users\risha\Desktop\PowerShell\testFile.csv
$columnName = $($Imported.b)
$Output = foreach ($i in $Imported) {
  foreach ($values in $columnName) {
    "{0} : {1:D13}" -f $i, [int] $values
  }
}
$Output
```

---

<div class="post-metadata">

### Author: ![rishabh-a](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@rishabh-a](https://forums.powershell.org/u/rishabh-a)
#### Post date: [March 3, 2017, 4:51am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/6 "2017-03-03T04:51:46Z")

</div>

Thank you so much sir. Code below worked for me.

```
$Imported = Import-Csv -Delimiter "," -Path C:\Users\risha\Desktop\PowerShell\testFile.csv
$columnName = $($Imported.b)
$Output = foreach ($i in $Imported) {
  foreach ($values in $columnName) {
    $formattedText = "{0:D13}" -f [int] $values
    $i.b = "$formattedText"
  }
  $i
}
$Output
```

---

<div class="post-metadata">

### Author: ![rishabh-a](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@rishabh-a](https://forums.powershell.org/u/rishabh-a)
#### Post date: [March 3, 2017, 6:50am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/7 "2017-03-03T06:50:54Z")

</div>

Sorry Sorry, This didn’t worked!

```
$Imported = Import-Csv -Delimiter "," -Path C:\Users\risha\Desktop\PowerShell\testFile.csv
$columnName = $($Imported.b)
$Output = foreach ($i in $Imported) {
  foreach ($values in $columnName) {
    $formattedText = "{0:D13}" -f [int] $values
    $i.b = "$formattedText"
  }
  $i
}
$Output
```

**It updates the whole column with the last value at $formattedText**

---

<div class="post-metadata">

### Author: ![skalizar](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@skalizar](https://forums.powershell.org/u/skalizar)
#### Post date: [March 3, 2017, 8:08am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/8 "2017-03-03T08:08:38Z")

</div>

```
$Imported = @'
a,b,c
1,2,3
4,5,6
'@ | convertfrom-csv

foreach ($i in $Imported) {
  $i.b = "{0:D13}" -f [int] $i.b
}
$Imported
```

---

<div class="post-metadata">

### Author: ![rishabh-a](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@rishabh-a](https://forums.powershell.org/u/rishabh-a)
#### Post date: [March 3, 2017, 9:55am UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/9 "2017-03-03T09:55:54Z")

</div>

Thank you so much Ron. Your solution worked as per my requirement.

```
$Imported = Import-Csv -Delimiter "," -Path C:\Users\risha\Desktop\PowerShell\testFile.csv
foreach ($i in $Imported) {
  $i.b = "{0:D13}" -f [int] $i.b
}
$Imported | Export-Csv "C:\Users\risha\Desktop\PowerShell\replace.csv" -NoT -Encoding "UTF8"
```

Regards,  
Rishabh

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/format-particulat-column-values-of-csv-file/8175/10 "2024-05-16T20:39:54Z")

</div>


