Fred
Fred

Reputation: 4076

Read file into line delimited array

I'm using the 32-bit version of PowerShell and running it via the PowerGUI Script Editor.

Everything online seems to think that Get-Content returns the file as an array; however,

$lines = Get-Content -Path $xmlFilename
Write-Host $lines.GetType()

outputs

System.String

I know how to do this using the builtin .Net types; is there a PoweShell v2.0 way of doing this?

Upvotes: 2

Views: 4515

Answers (1)

CB.
CB.

Reputation: 60918

Are you sure that $xmlFilename file have more than one line separed by `n or `r`n?

if the file contain just a single line you can do it:

 $lines = @(Get-Content -Path $xmlFilename) # this return [object[]] type

or

$lines = ,(Get-Content -Path $xmlFilename)

or

 $lines = [string[]](Get-Content -Path $xmlFilename) # this return [string[]] type

Upvotes: 3

Related Questions