Script to remove printers

by Jeremy Saunders on July 30, 2008

I wrote this script to help a customer cleanup user profiles and remove unsupported printers. This script can safely be executed in each user session and works a treat. It stamps the registry after the first time it runs, so that it can be left in place to capture all users, whilst only ever removing the printers once. A nice polite message box will appear to let the user know that their printers have been removed and tell them how to add new ones.

If, however, you find that some printers just cannot be removed, try my other script to remove orphaned/stale printer objects.


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 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
  End If
End If

'Clean Up Memory We Used
Set WshShell = Nothing
set WSHNetwork = Nothing
Set WSHPrinters = Nothing


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
  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
      If BlnReturn=False Then
      End If
    End If
  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
      Exit Function
    End If
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" & 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
Jeremy Saunders

Jeremy Saunders

Independent Consultant | Contractor | Microsoft & Citrix Specialist | Desktop Virtualization Specialist at J House Consulting
Jeremy is a highly respected, IT Professional, with over 30 years’ experience in the industry. He is an independent IT consultant providing expertise to enterprise, corporate, higher education and government clients. His skill set, high ethical standards, integrity, morals and attention to detail, coupled with his friendly nature and exceptional design and problem solving skills, makes him one of the most highly respected and sought after Microsoft and Citrix technical resources in Australia. His alignment with industry and vendor best practices puts him amongst the leaders of his field.
Jeremy Saunders
Jeremy Saunders
Jeremy Saunders

Previous post:

Next post: