Oxycash
Oxycash

Reputation: 197

Updating Excel columns(or rows?) based on a condition

I have an excel file with usernames, city. I am trying to add a new column to the same excel, lets say the column is Dept. I have differemnt list of users which tells me which user belong to which dept. I am now trying to update the Dept column in the original excel file I have. My current code as follows

$test = import-csv .\test.csv | Select Name,City,Dept
if ($test.name = 'John'){
$test.Dept = 'HR' 
}

It doesnt work, getting this error

Exception setting "Name": "The property 'Name' cannot be found on this object. Verify that the property exists and can be set."
At line:2 char:1
+ $test.Name= 'John'
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

Not sure where I am doing wrong and if this is even correct approach.

Upvotes: 1

Views: 195

Answers (1)

trampCoder
trampCoder

Reputation: 27

This might help you out here, use dept or any other column name, which you want to add instead of column3.

Import-Csv file.csv | 
Select-Object *,"column3" | 
Export-Csv file.csv -NoTypeInformation

Upvotes: 1

Related Questions