SOLVED

Import file as an array

Copper Contributor

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?

2 Replies
best response confirmed by Harm_Veenstra (MVP)
Solution
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?

@Harm_Veenstra 

 

Thanks, it is working now with your advise

1 best response

Accepted Solutions
best response confirmed by Harm_Veenstra (MVP)
Solution
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?

View solution in original post