Hey everyone. I'm very new to power shell and have written a script to iterate through a spreadsheet, removing duplicate email addresses from another multiple sheet workbook.
It works, but is very slow.
Can I fix this by using a function and "exit"? if so, how do I pass arguments into a function with powershell?
Thanks
P.S. not yet sure how to apply the code tags on this site....
(Please visit the site to view this media)
# Ray Winkelman 2014 © All Rights Reserved.
# This script iterates through an excel spreadsheet
param($Work)
# restart PowerShell with -noexit, the same script, and 1
if (!$Work) {
powershell -noexit -file $MyInvocation.MyCommand.Path 1
return
}
$strMailListPath = "C:\Mailer\MailingList.xls"
$strUnsubscribeListPath = "C:\Users\Doug\Desktop\Unsubscribers\UnsubscribeList.xls"
$objExcel = New-Object -comobject Excel.Application
$objExcel.Visible = $false
$objExcel.DisplayAlerts = $false
$objWorkbook = $objExcel.Workbooks.Open($strMailListPath)
$objWorkbookUns = $objExcel.Workbooks.Open($strUnsubscribeListPath)
write-host "Parsing Unsubscribe List."
$UnsRow = 1
$UnsSheet = 1
$row = 1
$sheet = 1
$objSheetUns = $objWorkbookUns.Sheets.Item($UnsSheet)
write-Host "Unsubscribe Worksheet: " $objSheetUns.Name
#Iterating through the -- UNS -- rows, stopping at the first blank in col 1
while ($objSheetUns.Cells.Item($UnsRow,1).Text -ne "")
{
write-Host "Checking For Unsubscriber: " $objSheetUns.Cells.Item($UnsRow,1).Text
while ($sheet -le $objWorkbook.Sheets.Count)
{
$objSheet = $objWorkbook.Sheets.Item($sheet)
#Iterating through the rows, stopping at the first blank in col 1
while ($objSheet.Cells.Item($row,1).Text -ne "")
{
if ($objSheetUns.Cells.Item($UnsRow,1).Text -eq $objSheet.Cells.Item($row,1).Text)
{
$range = $objSheet.Cells.Item($row, 1).EntireRow
$range.Interior.ColorIndex = 3
write-Host "Found: " $objSheetUns.Cells.Item($UnsRow,1).Text " In: " $objSheetUns.Name
write-Host "---"
}
$row += 1
}
$sheet += 1
$row = 1
}
$UnsRow += 1
$sheet = 1
}
Start-Sleep 5
$objWorkbookUns.Close()
$objWorkbook.SaveAs("C:\Mailer\MailingList.xls")
$objWorkbook.Close()
$objExcel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)
Remove-Variable objExcel