问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/17574163/read-file-into-line-delimited-array