If, however, you find that some printers just cannot be removed, try my other script to remove orphaned/stale printer objects.
Enjoy!
Option Explicit
' This script will enumerate all printers and then remove the ones that do not match an array of printers that
' should not be removed. Nor will it remove any Terminal Server or Citrix client printer mappings.
' It will tatoo the registry to ensure it only runs once per user, which makes it easy to manage for the future.
' Note that there are two separate subroutines. One to remove network printers, and another to remove local printers.
' IMPORTANT: This script DOES NOT remove the drivers.
' Written by Jeremy@jhouseconsulting.com on 8th July 2008
Dim WshShell, WSHNetwork, WSHPrinters, arrPrinters, LOOP_COUNTER, strPrinter, strKey, strValue
Dim strComputer, objWMIService, colInstalledPrinters, objPrinter
'These are the printers we DO NOT want to remove.
arrPrinters = Array("Send To OneNote 2007","Microsoft XPS Document Writer","Microsoft Office Document Image Writer","Adobe PDF","Generic / Text Only")
set WshShell = WScript.CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
strKey = "HKCU\Printers\"
If RegValueExists(strKey & "PrintersRemoved") Then
strValue = WshShell.RegRead(strKey & "PrintersRemoved")
If instr(lCase(strValue),"yes")<>1 Then
RemoveNetworkPrinters()
DeleteLocalPrinters()
UserMessageWithTimeout()
End If
Else
RemoveNetworkPrinters()
DeleteLocalPrinters()
UserMessageWithTimeout()
End If
'Clean Up Memory We Used
Set WshShell = Nothing
set WSHNetwork = Nothing
Set WSHPrinters = Nothing
wscript.quit(0)
Sub RemoveNetworkPrinters()
Dim BlnReturn
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
If Instr(WSHPrinters.Item(LOOP_COUNTER +1),"in session")=0 Then
BlnReturn=InArray(WSHPrinters.Item(LOOP_COUNTER +1),arrPrinters)
If BlnReturn=False Then
On Error Resume Next
WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
If Err.Number = -2147022646 Then
' wscript.echo "This is a local printer and cannot be removed using the RemovePrinterConnection method."
On Error Goto 0
End If
End If
End If
NEXT
WshShell.RegWrite strKey & "PrintersRemoved", "Yes - on " & date & " at " & time
End Sub
Sub DeleteLocalPrinters()
Dim BlnReturn
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
' query for installed local printers
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
If Instr(lcase(objPrinter.Name),"in session")=0 Then
BlnReturn=InArray(objPrinter.Name,arrPrinters)
If BlnReturn=False Then
objPrinter.delete_
End If
End If
Next
Set objWMIService = Nothing
End Sub
Function RegValueExists(sRegValue)
' Returns True or False based of the existence of a registry value.
Dim oShell, RegReadReturn
Set oShell = CreateObject("WScript.Shell")
RegValueExists = True ' init value
On Error Resume Next
RegReadReturn = oShell.RegRead(sRegValue)
If Err.Number <> 0 Then
RegValueExists = False
End if
On Error Goto 0
Set oShell = Nothing
End Function
Function InArray(item,myarray)
Dim i
For i=0 To UBound(myarray) Step 1
If lcase(item)=lcase(myarray(i)) Then
InArray=True
Exit Function
End If
Next
InArray=False
End Function
Sub UserMessageWithTimeout()
Dim strMessage, objShell, intReturn
Const wshOK = 1
Const wshYes = 6
Const wshNo = 7
Const wshOKButton = 0
Const wshCriticalMessage = 16
Set objShell = CreateObject("Wscript.Shell")
strMessage = "** Your printers have now been deleted. **" & VbCr & VbCr & _
"* You will need to reinstall your printers." & VbCr & _
"* To do this go to http://myiprint.mydomain.com.au/ipp" & VbCr & _
"* Note: Printer naming conventions have been modified." & VbCr & _
"* Note: Detailed printer locations have been added to help you locate your printers." & VbCr & VbCr & _
"Need Help?" & VbCr & _
"* The IT Helpdesk will be manned for any assistance users may require."
intReturn = objShell.Popup(strMessage, 60, "Printing Maintenance", wshOKButton + wshCriticalMessage)
' If intReturn = wshOK Then
' Wscript.Echo "You clicked the OK button."
' Else
' Wscript.Echo "The popup timed out."
' End If
Set objShell = Nothing
End Sub
One Trackback/Pingback
Post a Comment