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.

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
Jeremy Saunders

Jeremy Saunders

Technical Architect | DevOps Evangelist | Software Developer | Microsoft, NVIDIA, Citrix and Desktop Virtualisation (VDI) Specialist/Expert | Rapper | Improvisor | Comedian | Property Investor | Kayaking enthusiast at J House Consulting
Jeremy Saunders is the Problem Terminator. He is a highly respected IT Professional with over 35 years’ experience in the industry. Using his exceptional design and problem solving skills with precise methodologies applied at both technical and business levels he is always focused on achieving the best business outcomes. He worked as an independent consultant until September 2017, when he took up a full time role at BHP, one of the largest and most innovative global mining companies. With a diverse skill set, high ethical standards, and attention to detail, coupled with a friendly nature and great sense of humour, Jeremy aligns to industry and vendor best practices, which puts him amongst the leaders of his field. He is intensely passionate about solving technology problems for his organisation, their customers and the tech community, to improve the user experience, reliability and operational support. Views and IP shared on this site belong to Jeremy.
Jeremy Saunders
Jeremy Saunders

Previous post:

Next post: