Powershell : List number of lines in CSV files

I had a weird requirement today to count the number of lines in a bunch of CSV files in a folder, there we a couple of files but I needed a count of how many rows are in each CSV file, so of to the PowerShell cave we go, and I came up with this:

# Define the folder path containing CSV files
$folderPath = "<folder_path>"

# Get all CSV files in the folder
$csvFiles = Get-ChildItem -Path $folderPath -Filter "*.csv" | Sort-Object LastWriteTime

# Iterate through each CSV file
foreach ($file in $csvFiles) {

# Get the line count of the current CSV file
    $lineCount = (Get-Content -Path $file.FullName | Measure-Object -Line).Lines

# Output the file name and line count
    Write-Output "$($file.Name): $lineCount lines"
}

This will then output all the CSV files and the number of rows of data for each CSV file like this, obviously this will only work with formatted data where you need a line count, but its handy for quick results:



Previous Post Next Post

Ω†Ω…ΩˆΨ°Ψ¬ Ψ§Ω„Ψ§ΨͺΨ΅Ψ§Ω„