This is the first time that I'm writing a function that can call itself. I'm trying to remove all the columns of a DataTable where the rows are empty.
The code works fine but spits out some errors. For one reason or another it's looping one last time through the for loop with the old $Columns number still in memory. Although I'm filling it again each time the function is called... I don't understand why..
The code:
Function Remove-EmptyColumns {
$Columns = $DataTable.Columns.Count
$Rows = $DataTable.Rows.Count
for ($c = 0; $c -lt $Columns; $c++) {
$Empty = 0
for ($r = 0; $r -lt $Rows; $r++) {
if ($DataTable.Rows[$r].Item($c).ToString() -eq '') {
$Empty++
}
}
if ($Empty -eq $Rows) {
$DataTable.Columns.Remove($DataTable.Columns[$c])
Remove-EmptyColumns
}
}
}
Thank you for your help.