0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Most appreciated, seems so logical!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.