Home » excel » Loop through each Worksheet in Excel to perform tasks in PowerShell

Loop through each Worksheet in Excel to perform tasks in PowerShell

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to write a PowerShell script ( my first one ) that will loop through each worksheet in an Excel file and delete the 2nd row and then delete a range of cells. From other posts on StackOverflow, I have been able to piece together a script that will accomplish this task somewhat.

This task will delete the 2nd row on each worksheet and then delete the range of cells that is specified – but if another worksheet is added – then the script would have to modified to account for this.

$file = 'C:\SSIS\Email Report Content.xlsx'
$excel = new-object -com Excel.Application -Property @{Visible = $false} 
$workbook = $excel.Workbooks.Open($file) # Open the file
$sheet = $workbook.Sheets.Item(1) # Activate the first worksheet
[void]$sheet.Cells.Item(2, 1).EntireRow.Delete() # Delete the second row
$range = $sheet.Range("E2","I13")
$range.delete()
$workbook.Close($true) # Close workbook and save changes
$excel.quit() # Quit Excel
[Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM


$file = 'C:\SSIS\Email Report Content.xlsx'
$excel = new-object -com Excel.Application -Property @{Visible = $false} 
$workbook = $excel.Workbooks.Open($file) # Open the file
$sheet = $workbook.Sheets.Item(2) # Activate the first worksheet
[void]$sheet.Cells.Item(2, 1).EntireRow.Delete() # Delete the second row
$range = $sheet.Range("E2","I13")
$range.delete()
$workbook.Close($true) # Close workbook and save changes
$excel.quit() # Quit Excel
[Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM

$file = 'C:\SSIS\Email Report Content.xlsx'
$excel = new-object -com Excel.Application -Property @{Visible = $false} 
$workbook = $excel.Workbooks.Open($file) # Open the file
$sheet = $workbook.Sheets.Item(3) # Activate the first worksheet
[void]$sheet.Cells.Item(2, 1).EntireRow.Delete() # Delete the second row
$range = $sheet.Range("E2","I13")
$range.delete()
$workbook.Close($true) # Close workbook and save changes
$excel.quit() # Quit Excel
[Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM

What I would like to do here is to have the script be dynamic so that it would automatically pick up additional worksheets and perform the same task on each. I’ve tried to modify the logic I found at Experts-Exchange but have not been able to get it to work correctly.

$File = 'C:\SSIS\Email Report Content.xlsx'
$Excel = New-Object -ComObject Excel.Application -Property @{Visible = $False} 
$Workbook = $Excel.Workbooks.Open($File) # Open the file
$WorkSheet = $Workbook.WorkSheets

foreach ($WorkSheet in $Workbook.WorkSheets ) {

# $Sheet = $Workbook.Sheets.Item(1) # Activate the first worksheet
[void]$Sheet.Cells.Item(2, 1).EntireRow.Delete() # Delete the second row
$Range = $Sheet.Range("E2","I13")
$Range.delete()
$Workbook.Close($true) # Close workbook and save changes

}

$Excel.quit() # Quit Excel
[Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) # Release COM 

Could I get some assistance on how to handle this task? Thanks,

How to&Answers:

Was able to get this working with this code:

$file = 'C:\SSIS\Email Report Content.xlsx'
$excel = new-object -com Excel.Application -Property @{Visible = $false} 
$workbook = $excel.Workbooks.Open($file) # Open the file
foreach ($sheet in $workbook.Worksheets) {
    [void]$sheet.Cells.Item(2, 1).EntireRow.Delete() # Delete the second row
    $range = $sheet.Range("E2","I13")
    $range.delete()
}
$workbook.Close($true) # Close workbook and save changes
$excel.quit() # Quit Excel
[Runtime.Interopservices.Marshal]::ReleaseComObject($excel) # Release COM