Table of Contents
Using Get-Content
Cmdlet
Use the Get-Content
cmdlet to read file line by line in PowerShell.
1 2 3 |
Get-Content E:\Test\File1.txt |
1 2 3 4 5 6 7 8 9 |
This is line one. This is line two This is line 3. I am line 4. Its line 5. This is line 6. This is line 7. |
We used the Get-Content cmdlet to read the specified file line by line in PowerShell. It is used to get the content or data of the specified item; here, the specified item can be a text file or function.
For files, the Get-Content
cmdlet will read one line at a time and return the collection of objects where every object will represent the line of content.
If you are using PowerShell version 3.0 or above, you can also get a particular line number from the start or end of the file. We can also get the same results as above using Get-Content
with ForEach()
.
1 2 3 |
ForEach ($line in Get-Content E:\Test\File1.txt) {echo $line} |
1 2 3 4 5 6 7 8 9 |
This is line one. This is line two This is line 3. I am line 4. Its line 5. This is line 6. This is line 7. |
In the above code, we got all the content of the specified file using Get-Content
and then used the ForEach()
loop to iterate over each line of the retrieved content. For each iteration, we used echo
to print every line.
Usually, this approach (using Get-Content
with ForEach()
) is preferred where we need to manipulate every line. For example, we will use the concatenation operator (+
) in the echo
statement as follows if we want to add OK -
at the start of every line and then display it.
1 2 3 |
ForEach ($line in Get-Content E:\Test\File1.txt) {echo ("OK - " + $line)} |
1 2 3 4 5 6 7 8 9 |
OK - This is line one. OK - This is line two OK - This is line 3. OK - I am line 4. OK - Its line 5. OK - This is line 6. OK - This is line 7. |
Now, think of a situation where we need to read a specified file line by line, but for a specific number of lines from the start/end of the given text file, the following solution will work for us.
Use Get-Content
Cmdlet to Read Partial Data
To read a specified text file line by line in PowerShell:
- Use
Get-Content
to read the specified file line by line. - Use
Select-Object
with the-First
/-Last
parameter name to read the first/lastn
number of lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$file_content = Get-Content E:\Test\File1.txt $first_four_lines = $file_content | Select-Object -First 4 $last_four_lines = $file_content | Select-Object -Last 4 Write-Host @" First Four lines: $($first_four_lines) Last Four lines: $($last_four_lines) "@ |
1 2 3 4 5 6 |
First Four lines: This is line one. This is line two This is line 3. I am line 4. Last Four lines: I am line 4. Its line 5. This is line 6. This is line 7. |
In the previous section, we learned about the Get-Content
cmdlet. Here, we used it again to read content from the specified file, but this time, we saved the read data in the $file_content
variable.
Next, we forwarded the value of the $first_content
variable to the Select-Object
cmdlet using pipe (|
), which is used to forward data from the previous process to the following process.
The Select-Object
cmdlet is used to select object/object properties. We can also use it to choose a particular number of unique objects and objects from a specific position in an array.
This cmdlet allows us to use various parameters, for instance, -First
and -Last
, that select a particular number of objects from an array.
In our case, each line is an object and -First
selects the first four lines and returns them as an array. The -Last
performs the same but selects objects from the last.
The following solution is for us if we are looking for a way to read the whole file line by line but display those which match the particular pattern. Let’s learn this approach below.
Use Get-Content
Cmdlet with -Like
Operator
To read the given file line by line in PowerShell:
- After defining our pattern, use
ForEach()
to iterate over each line of a file that we read using theGet-Content
cmdlet. - In each iteration, use the
if
statement with the-Like
operator to search the specified pattern in the current line. - Within the
if
block, use theWrite-Output
cmdlet to print the current line if theif
condition is fulfilled.
1 2 3 4 5 6 7 8 9 |
foreach($current_line in Get-Content E:\Test\File1.txt) { if($current_line -like "*[0-9].") { Write-Output $current_line } } |
1 2 3 4 5 6 7 |
This is line 3. I am line 4. Its line 5. This is line 6. This is line 7. |
This code example is similar to the first section, where we used Get-Content
with the ForEach()
loop. The only difference is to use the if
statement with the -Like
operator to search for the given pattern and display the $current_line
if it fulfilled the if
condition.
The advantage of using the -Like
operator is that we do not need to escape the backslash. Now, what pattern are we using? We printed only those lines on PowerShell Console if they ended with any number immediately followed by a dot (.
).
Using switch
Statement
Use switch
to read the given text file line by line in PowerShell.
1 2 3 4 5 6 7 8 9 |
$regex = '^I' switch -regex -file E:\Test\File1.txt { $regex { "line is: $_" } } |
1 2 3 4 |
line is: I am line 4. line is: Its line 5. |
We used the switch with -Regex
and -File
parameters, where -File
was used to specify the file name for reading content and -Regex
was used to perform regex matching of a value to the condition.
In the above code example, we have written a custom regex value as I
, ensuring that the matching line starts with I
. If the specified regex condition is evaluated as Trues
, it would print the lines as shown in the output given above.
If you are wondering about $_
, do not worry; it is an automatic variable in PowerShell that holds the line for which the regex condition is satisfied.
Further reading:
Using the ReadLines()
of [System.IO.File]
Class
Use the ReadLines()
method of the [System.IO.File]
class to read the specified file line by line in PowerShell.
1 2 3 4 5 |
foreach($line in [System.IO.File]::ReadLines("E:\Test\File1.txt")){ Write-Output $line } |
1 2 3 4 5 6 7 8 9 |
This is line one. This is line two This is line 3. I am line 4. Its line 5. This is line 6. This is line 7. |
Using the ReadLines()
method of the [System.IO.File]
class is an excellent alternative to the Get-Content
cmdlet to read the text file (located in the given path) line by line.
Until now, we have covered various ways to read a file line by line, but what if we have any empty lines? Let’s see the following approach for learning how to handle blank lines.
Using the StreamReader
Class
To read a file line by line in PowerShell:
- Create a
StreamReader
object using theNew-Object
cmdlet. - Use the
while
loop to read one line at a time and check if it is not empty. - If the current line is not blank, print the line number along with the line’s text using the
Write-Host
cmdlet.
1 2 3 4 5 6 7 8 |
$stream_reader = New-Object System.IO.StreamReader{E:\Test\File1.txt} $line_number = 1 while (($current_line =$stream_reader.ReadLine()) -ne ''){ Write-Host "$line_number $current_line" $line_number++ } |
1 2 3 4 5 6 7 |
1 This is line one. 2 This is line two 3 This is line 3. 4 I am line 4. 5 Its line 5. |
Here, we created a StreamReader
object using New-Object cmdlet and saved it in $stream_reader
variable. Next, we declared a $line_number
and initialized it with 1
; we will use this variable to maintain the line numbers while reading.
Next, we used the .ReadLine()
method of the $stream_reader
object to read the specified file line by line; on each iteration, the $current_line
variable will be updated with the text returned by .ReadLine()
method. We can only move inside the while
if the $current_line
is not equal to an empty line.
If it is not an empty line, print the $line_number
along with $current_line
and increment the $line_number
by 1
; otherwise, exit from the while
loop. Remember, this loop will exist as soon as it finds an empty line. You must update the code according to your needs if you want to read all files, excluding blank lines.
You can also remove empty lines from file if you don’t them and read the file line by line by above methods.
That’s all about how to read file line by line in PowerShell.