I am trying to check the beginning of the first 2 lines of a text file.
$ascii = Get-Content -LiteralPath $path -Encoding ascii -TotalCount 2
if ($ascii[0].StartsWith("aa")) {}
if ($ascii[1].StartsWith("bb")) {}
This works fine except if the file has only 1 line. Then it seems to return a string rather than an array of strings, so the indexing pulls out a character, not a string.
Error if file has only 1 line:
Method invocation failed because [System.Char] doesn't contain a method named 'StartsWith'.
How can I detect if there are too few lines? $ascii.Length
is no help as it returns the number of characters if there is only one line!