Bulk delete printers

printer_delete

Right now I´m working on project to dispose two old print servers.  A issue with these servers is that no one used a routine to delete printers from the server when they physically powered off the printers mapped on the server. To solve this I used powershell to collect all printers from the server and then do a ping to the portname of each printer and list the ones who didn’t respond, in an array. Then I use that array to delete the printers from the server.

#-------------------------------------------------------------
#NAME: Delete_Printers.ps1
#AUTHOR: Viktor Lindström
#
#COMMENTS: Delete printers on remote server that don´t respond on ping.
#-------------------------------------------------------------

#Collect all printers
$AllPrinters = Get-Printer -ComputerName srv-print05 | where portname -Like "*prn*"

#create an empty array
$DownPrinters = @()

#collect printers that don't respond on ping 
foreach ($printer in $AllPrinters)
{
    $DownPrinters += Test-NetConnection $printer.PortName -ErrorAction SilentlyContinue | where pingsucceeded -eq $false
    Write-Host "testar ping mot" $printer.PortName
     
}

# Delete printers that didn´t respond on ping
foreach ($Printer2 in $DownPrinters)
{
$Getprint = Get-Printer -ComputerName srv-print05 | where portname -EQ $printer2.computername 
Remove-Printer -InputObject $Getprint
    
}