Skip to content

Instantly share code, notes, and snippets.

@matt40k
Created November 28, 2017 14:54
Show Gist options
  • Save matt40k/633e76f80cbe45a33fed596b8016ba7f to your computer and use it in GitHub Desktop.
Save matt40k/633e76f80cbe45a33fed596b8016ba7f to your computer and use it in GitHub Desktop.
Get the number of rows and columns from a CSV file
$file = "C:\tmp\test.csv"
$content = Get-Content -Path $file # -Delimiter ","
$noOfColumns = 0
$noOfRows = $content.Count # -1 # Remove header row
foreach ($line in $content)
{
$occurs = Select-String -InputObject $line -Pattern "," -AllMatches
$noOfOccurs = $occurs.Matches.Count + 1
if ($noOfOccurs -gt $noOfColumns)
{
$noOfColumns = $noOfOccurs
}
}
Write-Host "No of columns: $noOfColumns"
Write-Host "No of rows: $noOfRows"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment