Forum Discussion

dolphin168's avatar
dolphin168
Copper Contributor
May 02, 2024
Solved

Import file as an array

Hi,

 

I have the following in a script that is working fine using the following:

$string = @ ("xxx", "yyy", "zzz")

 

After I changed to the following that get from a file, it does not work:

$string = @()

$string += import-csv C:\temp\file.csv

 

The content of file.csv

xxx

yyy

zzz 

 

Can anyone advise how to resolve?

  • A .csv file should have headers, import-csv expects that to be there. It can be only one header... I added a Header called header on the first line...

    C:\Users\HarmVeenstra> $string = @ ("xxx", "yyy", "zzz")

    C:\Users\HarmVeenstra> $string.GetType()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Object[] System.Array

    C:\Users\HarmVeenstra> $string
    xxx
    yyy
    zzz
    C:\Users\HarmVeenstra> $string = @()
    C:\Users\HarmVeenstra> $string.GetType()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Object[] System.Array

    C:\Users\HarmVeenstra> $string += import-csv C:\temp\file.csv
    C:\Users\HarmVeenstra> $string.GetType()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Object[] System.Array

    C:\Users\HarmVeenstra> $string

    Header
    ------
    value1
    value2
    value3

    But... What is the problem exactly? What are you expecting?

2 Replies

  • A .csv file should have headers, import-csv expects that to be there. It can be only one header... I added a Header called header on the first line...

    C:\Users\HarmVeenstra> $string = @ ("xxx", "yyy", "zzz")

    C:\Users\HarmVeenstra> $string.GetType()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Object[] System.Array

    C:\Users\HarmVeenstra> $string
    xxx
    yyy
    zzz
    C:\Users\HarmVeenstra> $string = @()
    C:\Users\HarmVeenstra> $string.GetType()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Object[] System.Array

    C:\Users\HarmVeenstra> $string += import-csv C:\temp\file.csv
    C:\Users\HarmVeenstra> $string.GetType()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Object[] System.Array

    C:\Users\HarmVeenstra> $string

    Header
    ------
    value1
    value2
    value3

    But... What is the problem exactly? What are you expecting?