VBScript to Remove All Printers

Author Message
SiXXis

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 9/22/2009
  • Status: offline
VBScript to Remove All Printers Tuesday, September 22, 2009 10:24 PM (permalink)
0
Good Morning!
 
This is my code to remove all printers - please be carefull to stop and start your print spooler. It will remove all printers so please be careful. Add "if" statements if needed to remove only certain printers.
 
UNIVERSAL
 
'****************************************************************
'*            SiX 04/09/09 - Remove All Printers                *
'*--------------------------------------------------------------*
'*                                                              *
'* This script will remove all of your printers!!!              *
'*                                                              *
'*  I highly recommend you stop your print spooler service      *
'*  by using the "net stop spooler" command. I have not         *
'*  included this as I intended this script for a startup       *
'*  or shutdown script and not for just running in general use  *
'*                                                              *
'*  If you use "net stop spooler" then remember to restart      *
'*  the print spooler service with "net start spooler" after.   *
'*                                                              *
'****************************************************************

strComputer = "."
 
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

 
Set ColInstalledPrinters = objWMIService.ExecQuery _
 ("Select * from Win32_Printer")

 
Set WSHNetwork = wScript.CreateObject("wScript.Network")
 
set Printers = WSHNetwork.EnumPrinterConnections
On Error Resume Next
'Registry Entries
 
 Const HKEY_CLASSES_ROOT  = &H80000000
 Const HKEY_CURRENT_USER  = &H80000001
 Const HKEY_LOCAL_MACHINE = &H80000002
 Const HKEY_USERS         = &H80000003

 
 ' Object used to get StdRegProv Namespace
 Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")

 
 ' Object used to determine local machine name
 Set wshNetwork = CreateObject("WScript.Network")

 
 ' Registry Provider (StdRegProv) lives in root\default namespace.
 Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, "root\default")

 Set objRegistry = wmiNameSpace.Get("StdRegProv")

 sPath = "Printers\Connections"
 lRC = DeleteRegEntryUser(HKEY_CURRENT_USER, sPath)
 sPath = "SYSTEM\CurrentControlSet\Control\Print\Connections"
 lRC = DeleteRegEntryMachine(HKEY_LOCAL_MACHINE, sPath)
 Function DeleteRegEntryUser(sHive, sEnumPath)
'==============================
  ' Subkey Enumerator
     On Error Resume Next

     lRC = objRegistry.EnumKey(HKEY_CURRENT_USER, sEnumPath, sNames)
     For Each sKeyName In sNames
         lRC = objRegistry.DeleteKey(sHive, sEnumPath & "\" & sKeyName)
     Next
 End Function
'===============================================================
 '###########################################################################
 Function DeleteRegEntryMachine(sHive, sEnumPath)
'===========================
 
  ' Subkey Enumerator
     On Error Resume Next

     lRC = objRegistry.EnumKey(HKEY_LOCAL_MACHINE, sEnumPath, sNames)
     For Each sKeyName In sNames
         lRC = objRegistry.DeleteKey(sHive, sEnumPath & "\" & sKeyName)
     Next
 End Function
'===============================================================
'Network Machines
 For IntCount = 0 to Printers.Count - 1 Step 2
  WSHNetwork.RemovePrinterConnection Printers.Item(IntCount)
 Next
 
'Local Machines
 For Each objPrinter in ColInstalledPrinters
  objprinter.delete_
 Next
 
WITH IF STATEMENTS FOR MY NETWORK SPECIFICALLY!!!!!!!!!!!
 
'LCD 23/09/09 - Remove Server Printers
'-------------------------------------
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set ColInstalledPrinters = objWMIService.ExecQuery _
 ("Select * from Win32_Printer")
 
Set WSHNetwork = wScript.CreateObject("wScript.Network")
 
set Printers = WSHNetwork.EnumPrinterConnections
'We loop through each printer on the local machine and delete it only
'if the name of the printer starts with "file://wpbc-p/" (ie it is a print server
'printer)
On Error Resume Next
 
'Registry Entries
 
 Const HKEY_CLASSES_ROOT  = &H80000000
 Const HKEY_CURRENT_USER  = &H80000001
 Const HKEY_LOCAL_MACHINE = &H80000002
 Const HKEY_USERS         = &H80000003
 
 ' Object used to get StdRegProv Namespace
 Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
 
 ' Object used to determine local machine name
 Set wshNetwork = CreateObject("WScript.Network")
 
 ' Registry Provider (StdRegProv) lives in root\default namespace.
 Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, "root\default")
 Set objRegistry = wmiNameSpace.Get("StdRegProv")
 sPath = "Printers\Connections"
 lRC = DeleteRegEntryUser(HKEY_CURRENT_USER, sPath)
 sPath = "SYSTEM\CurrentControlSet\Control\Print\Connections"
 lRC = DeleteRegEntryMachine(HKEY_LOCAL_MACHINE, sPath)
 Function DeleteRegEntryUser(sHive, sEnumPath)
'=======================================
  ' Subkey Enumerator
     On Error Resume Next
 
     lRC = objRegistry.EnumKey(HKEY_CURRENT_USER, sEnumPath, sNames)
 
     For Each sKeyName In sNames
 
   'REMOVE ONLY THE WPBC-PRINT01 AND 02!!!!
 
   if left(sKeyName, 14) = ",,WPBC-PRINT01" or left(sKeyName, 14) = ",,WPBC-PRINT02" then
          lRC = objRegistry.DeleteKey(sHive, sEnumPath & "\" & sKeyName)
 
   end if
     Next
 End Function
'=========================================================================
 '##################################################################
 Function DeleteRegEntryMachine(sHive, sEnumPath)
'=====================================
 
  ' Subkey Enumerator
     On Error Resume Next
 
     lRC = objRegistry.EnumKey(HKEY_LOCAL_MACHINE, sEnumPath, sNames)
 
     For Each sKeyName In sNames
 
   'REMOVE ONLY THE WPBC-PRINT01 AND 02!!!!    
 
   if left(sKeyName, 14) = ",,WPBC-PRINT01" or left(sKeyName, 14) = ",,WPBC-PRINT02" then
          
          lRC = objRegistry.DeleteKey(sHive, sEnumPath & "\" & sKeyName)
 
   end if
 
     Next
 End Function
'=========================================================================
'Network Machines
 For IntCount = 0 to Printers.Count - 1 Step 2
 
  if ucase(Left(Printers.item(IntCount + 1), 8)) = "file://wpbc-p/" then
 
   WSHNetwork.RemovePrinterConnection Printers.Item(IntCount)
 
  end if
 
 Next
 
'Local Machines
 For Each objPrinter in ColInstalledPrinters
  tempstring = trim(objprinter.name)
  TempString = left(tempstring, 8)
  TempString = ucase(tempstring)
 
  if tempstring = "file://wpbc-p/" then
 
   objprinter.delete_
 
  end if
 
  tempstring = trim(objprinter.name)
  TempString = left(tempstring, 2)
  TempString = ucase(tempstring)
 
  if tempstring = "1-" then
 
   objprinter.delete_
 
  end if
 
  if tempstring = "0-" then
 
   objprinter.delete_
 
  end if
 
  if tempstring = "2-" then
 
   objprinter.delete_
 
  end if
 
  if tempstring = "3-" then
 
   objprinter.delete_
 
  end if
 
 Next
 
 
 
 
#1
    rasimmer

    • Total Posts : 2363
    • Scores: 163
    • Reward points : 0
    • Joined: 3/19/2009
    • Location: Cedar Rapids, IA
    • Status: offline
    Re:VBScript to Remove All Printers Wednesday, September 23, 2009 1:37 AM (permalink)
    0
    I think you posted this in the wrong forum, assuming you are sharing this script, it should be in the "Post a VBScript" forum.
     
    #2

      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2012 ASPPlayground.NET Forum Version 3.9