Reading All Text

by Apr 7, 2014

You can use Get-Content to read in any plain text file. However, Get-Content will return the file content line by line, and you get back a string array. The line endings are consumed.

To read in a text file in one big chunk, beginning with PowerShell 3.0, you can use the –Raw parameter (which coincidentally speeds up reading the file quite a bit, too).

So this gives you an array of string lines:

The Length property returns the number of lines in the file.

And this reads in the entire content in one big chunk, returning a single string:

This time, the length is the number of overall bytes, and reading the file is much faster (albeit consumes more memory, too).

Which approach is right? It depends on what you want to do with the data.

Twitter This Tip! ReTweet this Tip!