How to Count Lines in a File in PowerShell?

Recently only, while working on a file system requirement in PowerShell, I was required to count number of lines presented in a file using PowerShell. I tried different methods. In this PowerShell tutorial, we’ll explore various methods to count lines in a file using PowerShell with examples.

To count lines in a file using PowerShell, you can use the Get-Content cmdlet combined with Measure-Object. For example, $lineCount = (Get-Content ‘C:\path\to\file.txt’ | Measure-Object -Line).Lines will give you the total number of lines in ‘file.txt’. This method is efficient for files of any size and is the standard approach in PowerShell for line counting tasks.

Method 1: Using Get-Content and Measure-Object

The simplest and most straightforward method to count lines in a file in PowerShell is by using the Get-Content cmdlet to read the file and piping the output to the Measure-Object cmdlet to count the lines.

Here is the complete script.

$filePath = "C:\MyFolder\file.txt"
$lineCount = (Get-Content $filePath | Measure-Object -Line).Lines
Write-Host "The file has $lineCount lines."

In this script, Get-Content reads the file at the specified path, and Measure-Object -Line calculates the number of lines. The .Lines property gives us the actual count, which we then output.

I executed the above script using VS code, and you can see the output in the screenshot below:

Count Lines in a File in PowerShell

Method 2: Streaming Get-Content for Large Files

For larger files, the above method might consume a lot of memory as Get-Content reads the entire file into memory. To handle large files more efficiently, you can stream the file content and count lines one by one. This method is slower but uses less memory.

$filePath = "C:\path\to\your\largefile.txt"
$lineCount = 0
Get-Content $filePath -ReadCount 0 | ForEach-Object { $lineCount++ }
Write-Host "The file has $lineCount lines."

By using -ReadCount 0, Get-Content streams the file line by line, and the ForEach-Object loop increments the line count for each line.

Method 3: Using a PowerShell Script for Multiple Files

If you need to count the lines in multiple text files within a folder, a PowerShell script can be used to iterate through each file and count the lines.

$folderPath = "C:\MyFolder"
Get-ChildItem $folderPath -Filter *.txt | ForEach-Object {
    $file = $_
    $lineCount = (Get-Content $file.FullName | Measure-Object -Line).Lines
    Write-Host "The file $($file.Name) has $lineCount lines."
}

Here, Get-ChildItem retrieves all .txt files in the specified folder, and for each file, we count the lines using the Get-Content and Measure-Object cmdlets.

You can see the output in the screenshot below; it shows how many lines are presented in each text file inside the folder.

How to Count Lines in a File in PowerShell

Method 4: Counting Lines with Get-Content and Select-Object

Another way to count lines in a file in PowerShell is by using Get-Content in combination with Select-Object.

Here is the complete PowerShell script to count lines presented in a file.

$filePath = "C:\MyFolder\file.txt"
$lineCount = (Get-Content $filePath | Select-Object -Last 1).LineNumber
Write-Host "The file has $lineCount lines."

This method is particularly useful when you need to retrieve and display a certain line from the file, not just count the total number of lines.

Method 5: Quick Count for a Folder

For a quick line count of each file in a folder using PowerShell without the need for detailed output, you can use a simple one-liner script.

$folderPath = "C:\MyFolder"
Get-ChildItem $folderPath -Recurse | ForEach-Object { (Get-Content $_.FullName | Measure-Object -Line).Lines }

This script will output just the line counts for each file found in the specified folder and its subfolders.

Handling Empty Lines

If you want to exclude empty lines from the count, you can filter them out using the Where-Object cmdlet in PowerShell.

$filePath = "C:\MyFolder\file.txt"
$lineCount = (Get-Content $filePath | Where-Object { $_.Trim() -ne "" } | Measure-Object -Line).Lines
Write-Host "The file has $lineCount non-empty lines."

Conclusion

In this PowerShell tutorial, I have explained different methods to count lines in a file in PowerShell like Get-Content, Measure-Object, and Get-ChildItem, etc.

You may also like: