0

So I have a file which is an external variable. I use a PowerShell script to read it into an array to further process each line.

However, sometimes if the file has only one line, then the array will automatically read the file character-by-character.

Example - input file with only one row:

U09

Code:

$file = get-content $INPUT FILE
Write-Host $file[0]

Then I get the result U instead of U09

How do I avoid this?

G42
  • 9,791
  • 2
  • 19
  • 34
ella
  • 153
  • 1
  • 12
  • 4
    `$file = @(get-content $INPUT_FILE)` - wrap in `@()` to force it to be an array even when there's only one line – TessellatingHeckler Jul 01 '17 at 00:19
  • @TessellatingHeckler Nice. Please consider posting as answer so this can be marked as answered and removed from unanswered queue. – G42 Jul 01 '17 at 14:13
  • 1
    @gms0ulman It's a common powershell question, I was sure it was a duplicate, but couldn't find one quickly at the time. I've found one now and voted for it to be a dupe of that one instead of answering here. – TessellatingHeckler Jul 01 '17 at 19:18
  • @TessellatingHeckler just tried. perfect. thank you very much! – ella Jul 02 '17 at 20:09

0 Answers0