Table of Contents
Using Get-Content Cmdlet
We can use multiple ways to split the specified text file into an array based on every new line. To practice those methods and commands, we will use the file.txt file having the following content. Of course, you can also create or use your text file.
|
1 2 3 4 5 6 |
This is line one. This is line two. This is three and four on the same line. We didn't write it on a new line. This is line five. |
Let’s continue learning the solutions below.
Use the Get-Content cmdlet to split a text file into an array based on every new line.
|
1 2 3 4 5 |
$lines = Get-Content -Path .\file.txt $lines.GetType() $lines |
|
1 2 3 4 5 6 7 8 9 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array This is line one. This is line two. This is three and four on the same line. We didn't write it on a new line. This is line five. |
We used the Get-Content cmdlet to retrieve the content of a text file that we specified. Using this cmdlet is one of the most straightforward approaches because this, by default, returns every line of text as the string object. As a result, the PowerShell object’s collection becomes an array of the string object. So, we do not have to split it; it is already done by the Get-Content cmdlet, which we saved in the $lines variable.
We used the -Path parameter to specify the item’s path where Get-Content will get the content. In our case, it is file.txt located in the current directory (represented with .); you can use the complete path of your file as well. Remember, the paths must point to items, not containers, which means we can mention a path to one or multiple files but not to a directory.
To ensure that we have successfully split the text file into an array based on each new line, we chained the GetType() method with the $lines variable. Yes, we have done it; as you can see, the BaseType is System.Array in the above output. We can print an item of a specific index as $lines[2]; note that the array indexes start with 0 and ends at n-1 where the n is the size of an array.
Now think if you have a specific requirement to read file.txt as a single string and then split them based on each new line, then you must use the -Raw parameter as demonstrated in the following example. You can find other useful parameters here.
|
1 2 3 4 5 6 |
$string = Get-Content -Path .\file.txt -Raw $lines = $string -split "`n" $lines.GetType() $lines |
|
1 2 3 4 5 6 7 8 9 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array This is line one. This is line two. This is three and four on the same line. We didn't write it on a new line. This is line five. |
We used the -Raw parameter to ignore newline characters and get the entire data of file.txt as one string with the newline characters preserved, which we saved in the $string variable. Next, we used the -Split operator with a newline as a separator (delimiter) to split the text file into an array based on every newline.
Similarly, we can also use the .Split() method as follows:
|
1 2 3 4 5 6 |
$string = Get-Content -Path .\file.txt -Raw $lines = $string.Split("`n") $lines.GetType() $lines |
|
1 2 3 4 5 6 7 8 9 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array This is line one. This is line two. This is three and four on the same line. We didn't write it on a new line. This is line five. |
The -Split and .Split() are used to split a string into an array of substrings based on the given separator. In PowerShell, these are used interchangeably, but -Split is preferred because it is shorter and easy to type. However, if you are splitting the string based on complex and challenging delimiters, it is better to use the .Split() method.
The
-Rawparameter will only work in file system drives.
Further reading:
Using System.IO.File.ReadAllLines Method
Use the System.IO.File.ReadAllLines() method to split a text file into an array based on every new line.
|
1 2 3 4 5 |
$lines = [System.IO.File]::ReadAllLines("E:\Test\file.txt") $lines.GetType() $lines |
|
1 2 3 4 5 6 7 8 9 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String[] System.Array This is line one. This is line two. This is three and four on the same line. We didn't write it on a new line. This is line five. |
Here, we used the .ReadAllLines() method of the File class; it is also a one-line solution like the Get-Content cmdlet that we learned in the first example in this article. The .ReadAllLines() method took the complete path of the file.txt, read all the lines and returned them as a string array, which we saved in the $lines variable.
The good thing about the .ReadAllLines() method is that it also closes the file after reading all content, but you may have to face a few exceptions based on a few reasons that you find here.
That’s all about how to split text file on Newline in PowerShell.