PowerShell and Space in Expression

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

I have the following code below. However, I'm not sure how to handle this space between t

and N

.

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

Import-Csv .\Downloads\addata.csv | Select @{n="Name";e={$_.First Name}}

      

+3


source to share


1 answer


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

.

However, you probably want to keep the existing columns so that you can use the cmdlet Get-Content

to retrieve the content and use a simple regex to replace the column on the first line:



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

      

Note: . You can specify the encoding on the cmdlet Set-Content

.

+5


source







All Articles