Public/Remove-specPrinters.ps1

Function Remove-specPrinters {
    [cmdletBinding()]

    param (
        [Parameter(Mandatory = $true)]
        [System.Object[]]$allPrintersToRemove
    )

    #region 6. Delete printers
    if ((Get-specNumberOfItems $allPrintersToRemove) -gt 0) {
        Write-Host "Removing any printers set to 'Delete'" -ForegroundColor DarkCyan
        Write-Host '-------------------------------------' -ForegroundColor DarkCyan
    }

    $count = 0

    foreach ($printerToRemove in $allPrintersToRemove) {
        # Extract the printer name as we will be using this in a few places
        # $printerName = $printerToRemove.Printer_Name
        #$printerIP = $printerToRemove.Printer_Connection
        #$printerPORT = "TCPPort:$printerIP"


        # Extract the printer name, checking for both 'Printer_Name' and 'name'
        if ($printerToRemove.Printer_Name) {
            $printerName = $printerToRemove.Printer_Name
        } else {
            $printerName = $printerToRemove.Name
        }

        # Extract the printer connection, checking for both 'Printer_Connection' and 'Portname'
        if ($printerToRemove.Printer_Name) {
            $printerIP = $printerToRemove.Printer_Connection
        } else {
            $printerIP = $printerToRemove.PortName
        }

        # Extract the printer connection, checking for both 'Printer_Connection' and 'Portname'
        if ($printerToRemove.Port_Name) {
            $printerPORT = "TCPPort:$printerIP"
        } else {
            $printerPORT = $printerToRemove.PortName
        }


        $count++
        Write-specAlignedStrings -String1 'Processing:' -String1Value "$($count) of $(Get-specNumberOfItems $allPrintersToRemove)" `
            -String2 'Printer:' -String2Value $($printername) `
            -String3 'Port:' -String3Value $($printerPORT)


        # Instantiate the Printer object using New-specPrinterObject
        $printer = New-specPrinterObject -Name $printerName -PortName $printerPORT

        # Check if the printer exists before attempting to remove it
        if ($printer.PrinterExists()) {
            try {
                # Remove the printer
                $printer.RemovePrinter()
                sleep 5

                # Remove the port (if it exists)
                if ($printer.PortExists()) {
                    Restart-Service spooler
                    $printer.RemovePort()
                    Write-Host "Printer '$printerName' and its associated port removed successfully. Driver retained." -ForegroundColor DarkGreen
                } else {
                    Write-Host "Printer '$printerName' removed successfully. Port not found." -ForegroundColor DarkYellow
                }
            } catch {
                Write-Warning "Failed to remove printer '$printerName' or its port: $_"
            }
        } else {
            Write-Host "Printer '$printerName' not found. Skipping removal." -ForegroundColor DarkGray
        }
    }
    #endregion 6. Delete printers
}