PowerShell打印管理脚本优化及反馈功能添加需求问询
Hey there! Your initial script already hits the core of what you need to do—great start! Let's break down how to refine it for better maintainability and add clear feedback so you know exactly what's happening during execution.
First, let's clean up the exclusion logic by putting the allowed terms into a single variable. This makes it way easier to update later if you need to add/remove exceptions:
# Define allowed printer-related terms (case-insensitive match) $allowedTerms = "PDF|txt|fax|usb|enhanced|epson|microsoft"
Next, let's modify each section to capture the objects before deletion, then report on success/failure. We'll use Try/Catch blocks to handle any errors gracefully and log what couldn't be removed:
Printer Removal with Feedback
Write-Host "`n=== Removing Unwanted Printers ===" -ForegroundColor Cyan $printersToRemove = Get-Printer | Where-Object { $_.Name -notmatch $allowedTerms } if ($printersToRemove.Count -eq 0) { Write-Host "No printers found to remove." -ForegroundColor Green } else { foreach ($printer in $printersToRemove) { try { Remove-Printer -InputObject $printer -ErrorAction Stop Write-Host "Successfully removed printer: $($printer.Name)" -ForegroundColor Green } catch { Write-Host "Failed to remove printer: $($printer.Name) - Error: $($_.Exception.Message)" -ForegroundColor Red } } }
Printer Driver Removal with Feedback
Write-Host "`n=== Removing Unwanted Printer Drivers ===" -ForegroundColor Cyan $driversToRemove = Get-PrinterDriver | Where-Object { $_.Name -notmatch $allowedTerms } if ($driversToRemove.Count -eq 0) { Write-Host "No printer drivers found to remove." -ForegroundColor Green } else { foreach ($driver in $driversToRemove) { try { Remove-PrinterDriver -InputObject $driver -ErrorAction Stop Write-Host "Successfully removed driver: $($driver.Name)" -ForegroundColor Green } catch { Write-Host "Failed to remove driver: $($driver.Name) - Error: $($_.Exception.Message)" -ForegroundColor Red } } }
Printer Port Removal with Feedback
Write-Host "`n=== Removing Unwanted Printer Ports ===" -ForegroundColor Cyan $portsToRemove = Get-PrinterPort | Where-Object { $_.Name -notmatch $allowedTerms } if ($portsToRemove.Count -eq 0) { Write-Host "No printer ports found to remove." -ForegroundColor Green } else { foreach ($port in $portsToRemove) { try { Remove-PrinterPort -InputObject $port -ErrorAction Stop Write-Host "Successfully removed port: $($port.Name)" -ForegroundColor Green } catch { Write-Host "Failed to remove port: $($port.Name) - Error: $($_.Exception.Message)" -ForegroundColor Red } } }
Additional Tips:
- Run as Administrator: Printer/driver/port operations usually require elevated privileges, so make sure you run the PowerShell window as admin.
- Case Insensitivity: The
-notmatchoperator is already case-insensitive, so you don't have to worry about "EPSON" vs "epson". - Dry Run Option: If you want to test what would be removed without actually deleting anything, you can comment out the
Remove-*lines and just print the list of objects to remove.
This approach gives you clear, color-coded feedback for each operation, lets you easily update allowed terms, and handles errors so you know exactly what went wrong if something fails.
备注:内容来源于stack exchange,提问作者Sharpe

