Powershell Convert String[] to List<String>
powershell
Solution
No, you are right. As shown here, `[IO.File]::ReadAllLines` does return a `String[]` object. The confusing error that you are seeing is explained in @mjolinor's answer (I won't repeat it here).
Instead, I will tell you how to fix the problem. To convert a `String[]` object into a `List<String>` object in PowerShell, you need to explicitly cast it to such:
PS > [string[]]$array = "A","B","C"
PS > $array.Gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String[] System.Array
PS >
PS > [Collections.Generic.List[String]]$lst = $array
PS > $lst.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True List`1 System.Object
PS >
In your specific case, the code would be:
$csvUserInfo = [IO.File]::ReadAllLines($script:EmailListCsvFile)
[Collections.Generic.List[String]]$x = $csvUserInfo
Problem
I have this code: ``` $csvUserInfo = @([IO.File]::ReadAllLines($script:EmailListCsvFile)) $x = $csvUserInfo.ToList() ``` When it runs, I get this error: ``` Method invocation failed because [System.String] does not contain a method named 'ToList'. ``` Why is $csvUserInfo of type String? Doesn't [IO.File]::ReadAllLines return a string[] ? I've tried with/without the @ as well, it makes no difference.