Notice: Due to size constraints and loading performance considerations, scripts referenced in blog posts are not attached directly. To request access, please complete the following form: Script Request Form Note: A Google account is required to access the form.
Disclaimer: I do not accept responsibility for any issues arising from scripts being run without adequate understanding. It is the user's responsibility to review and assess any code before execution. More information

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

نموذج الاتصال