Search code examples
excelpowershelldelete-row

Delete rows with specific values


I'm using this part of my script to delete each row where there is value : admin

$i = 1

Do {
    If ($worksheet.Cells.Item($i, 1).Value() -eq 'admin') 
      {
            $objRange = $worksheet.Cells.Item($i, 5).EntireRow
            $objRange.Delete()
        $i -= 1 
      }
      $i += 1
}

While ($worksheet.Cells.Item($i,1).Value() -ne $null)

I would like to know how can I add some different values and delete more rows with words like : system, computer ...

Thanks !


Solution

  • Use the -in collection containment operators:

    $rowValuesToDelete = 'admin','system','computer'
    
    if($worksheet.Cells.Item($i, 1).Value() -in $rowValuesToDelete) {
      # ...
    }