3

I have a field in a CSV that is First Name. However, I want to rename that to Name.

I have the following code below. However, I am not sure how to handle that space between the t and N.

I've tried using quotes and some other tricks but so far none have worked. What do I need to do to get the values?

Import-Csv .\Downloads\addata.csv | Select @{n="Name";e={$_.First Name}}
Martin Brandl
  • 47,498
  • 11
  • 97
  • 129
Jason
  • 745
  • 1
  • 8
  • 23

1 Answers1

5

You can access variables with a space using single quotes, e. g. $_.'First Name'.

However, you probably want to keep the existing columns so you might use the Get-Content cmdlet to retrieve the content and use a simple regex to replace the column within the first line:

$content = Get-Content '.\Downloads\addata.csv'
$content[0] = $content[0] -replace 'First Name', 'Name'
$content | Set-Content '.\Downloads\addata.csv'

Attention: You may want to specify the encoding within the Set-Content cmdlet.

Martin Brandl
  • 47,498
  • 11
  • 97
  • 129