I did a powershell script that remove network printer. It is run with a scheduled deployed by GPP in user preferences that runs at any session logon.
Here is the script. It does search for printer with a network hostaddress that does equal the var $port but does not have the name of the var $imprimante The script does look for printer with TCP/IP port and WSD port
get-date > C:\Temp\debug.txt
###Variables###
$imprimante = "001 - HP LaserJet Pro M400 color M451dn (Salle prof)"
$port = "10.26.57.21"
###End###
Write-Host "Start Standard TCP/IP Printer"
Get-WmiObject win32_tcpipprinterport | select name, hostaddress | %{
$printer = New-Object System.Object
$printer | Add-Member -type NoteProperty -Name Name -Value $_.name -PassThru | Add-Member -type NoteProperty -Name HostAddress -Value $_.HostAddress
if ($printer.hostaddress -like $port)
{
$name = $printer.name
$p = Get-WmiObject win32_Printer -Filter "PortName like '$name' and not (Name like '$imprimante')"
$count = $p | measure
if ($count.count -gt 0)
{
$pn = $p | Select property -ExpandProperty Name
Write-Host "The printer '$pn' is going to be removed."
$p.delete()
}
}
}
Write-Host "End Of Standard TCP/IP Printer"
Write-Host "Start Of WSD Printer"
Get-WmiObject win32_Printer -filter "PortName like 'wsd%'" | %{
$p = $_
$pn = $_.name
$pc = $pn | measure
if ($pc.count -gt 0)
{
$address = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Print\Printers\$pn\PrinterDriverData\ -Name HPEWSIPAddress).HPEWSIPAddress
if ($address -like "$port*" -and $pn -notlike "$imprimante")
{
Write-Host "The printer '$pn' is going to be removed."
$p.delete()
}
}
}
Write-Host "End Of WSD Printer"
So far, the script runs well, the Scheduled Task also runs, but there is a persistent problem. The last printer to be deleted (or the printer if there is only one) does still shows up in "devides and printers".
The printer seems to be "uninstalled" because it is no more usable. Here is a picture before the script (Sorry french environnement)Printer before remove
Here is a picture after the scriptPrinter after remove
All options about printing is gone but the printer still remain. The only to get rid of it is to delete it manually.
Anyone have has ever seen this? I've tryed to remove the printer with "Rundll printui.dll" and did not succeed.
Thanks alot