0

I've been fighting for over an hour for something that seems simple to me ... But I can not do it

Explanation :

I import a CSV who look like this, he's empty :

DAYS | PC1 | PC2

And i have a Variable "$Days" who look like this :

PS C:\Users> $Days
12/02/2019
13/02/2019
14/02/2019
15/02/2019
16/02/2019

All i want is to add every line to the "Days" column...

Like :

   DAYS    | PC1 | PC2
12/02/2019 |     |
13/02/2019 |     |
14/02/2019 |     |
15/02/2019 |     |
16/02/2019 |     |

I try something like :

Foreach ($row in $Days){
    $CSV.Days = $row
}

But he tell me that the property "Days" is not found.

i also try something like this :

$CSV | Select-Object @{Name='Days';Expression={$forearch ($row in $Days){$row}}},*

But no result again, i don't know what i'm doing wrong...

Thank you in advance for your help.

4
  • If your CSV really looks like that your first issue might be you need to use -delimiter '|' with Import-CSV Commented Feb 7, 2019 at 13:22
  • No, it's ok in reality it look like : "days";"pc1";"pc2" and i use -Delimiter ";" Commented Feb 7, 2019 at 13:24
  • 2
    $Days| ForEach-Object{[PSCustomObject]@{DAYS=$_;PC1="";PC2=""}} Commented Feb 7, 2019 at 13:39
  • 1
    @LotPings can you propose an answer, because if not, the question will still unanswered and you wrote a solution ;) Commented Feb 7, 2019 at 13:50

2 Answers 2

1

The problem with your approach is that the file is empty thus no properties are generated for $CSV

A simple solution is generating the $CSV by iterating $Days and building a [PSCustomObject]

$Days = @'
12/02/2019
13/02/2019
14/02/2019
15/02/2019
16/02/2019
'@ -split '\r?\n'

$CSV = $Days | ForEach-Object{
    [PSCustomObject]@{
        DAYS = $_
        PC1  = $NULL
        PC2  = $NULL
    }
}

$CSV
$CSV | Export-Csv .\YouNameIt.csv -NoTypeInformation
Sign up to request clarification or add additional context in comments.

Comments

1

Or, a shorter version:

$CSV-out = $csv | ForEach-Object{[PSCustomObject]@{DAYS = $_}}|Select days, PC1, PC2

$csv is a list of values. Similar to

$csv = @("12/02/2019","13/02/2019","14/02/2019","15/02/2019","16/02/2019")

If you then do a $CSV | FT

    DAYS       PC1 PC2
    ----       --- ---
12/02/2019         
13/02/2019
14/02/2019
15/02/2019
16/02/2019

You don't have to include a custom object just to add fields. You can do a

select

and it will add them for you as well.

Comments

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.