Mr Deans
Mr Deans

Reputation: 369

Powershell CSV import and update IF function

I have a .csv file that I wish to work with in Powershell. The .csv has three columns

AccountNumber1, AccountNumber2, Order

The data can be in a number of variables

Now the data is non consistent, There will always be an AccountNumber2. However If AccountNumber1 exists then this is the account number that needs to be used.

enter image description here

Other than the import-csv options I have no ideas on how to progress this, I wondered about if length = 0 or something around this but completely unsure.

Easy to do in excel but I would prefer not to write an excel function in as feel that its an extra layer of complexity this shouldn't really need.

Import-Csv -literalpath 'L:\SList.txt' -Headers AccountNumber1,AccountNumber2,CorrectAC,Order

Excel would simply be; =IF(A2="",B2,A2) obviously but how this is completed in PS?

Upvotes: 0

Views: 493

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

Use calculated properties to replace empty AccountNumber1 fields:

$headers = 'AccountNumber1','AccountNumber2','CorrectAC','Order'
Import-Csv -literalpath 'L:\SList.txt' -Headers $headers |
    Select-Object -Property *,@{n='AccountNumber1';e={
        if (! $_.AccountNumber1) {$_.AccountNumber2}
    }} -Exclude AccountNumber1

Upvotes: 1

Related Questions