title: Delete Windows 7 network printer driver remotely using WMI
body: <p>I need some help with deleting network printer driver remotely on a <strong>Windows 7 client machine</strong> using a vbscript with an account having administrator privileges (Elevated Account) on the remote computer. The problem is that I can't delete the connected printer the user have connected. Everything else seems to work. Below is the code for the script.</p>
  
  <p>The script does several things, but the ultimate goal is to physically remove the printer-drivers. The current version of the script fails since the driver files are in use. The script contains code to avoid deleting special printers. It also stops and starts the print spooler. </p>
  
  <pre><code>intSleep        = 4000
  strService      = " 'Spooler' "
  strComputer         = "&lt;remote computer name&gt;"
  Set fsobj       = CreateObject("Scripting.FileSystemObject") 'Calls the File System Object
  Set objNetwork      = CreateObject("WScript.Network")
  arrPrinters         = Array("PDF", "Adobe", "Remote", "Fax", "Microsoft", "Send To", "Generic")
  
  Set objWMIService = GetObject("winmgmts:" _
      &amp; "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2")
  
  '   List drivers
  Set colInstalledPrinters =  objWMIService.ExecQuery _
      ("Select * from Win32_PrinterDriver")
  
  Set drivrutinCol  = CreateObject("Scripting.Dictionary")
  
  For each objPrinter in colInstalledPrinters
  '    Wscript.Echo "Configuration File: " &amp; objPrinter.ConfigFile
  '    Wscript.Echo "Data File: " &amp; objPrinter.DataFile
  '    Wscript.Echo "Description: " &amp; objPrinter.Description
  '    Wscript.Echo "Driver Path: " &amp; objPrinter.DriverPath
  '    Wscript.Echo "File Path: " &amp; objPrinter.FilePath
  '    Wscript.Echo "Help File: " &amp; objPrinter.HelpFile
  '    Wscript.Echo "INF Name: " &amp; objPrinter.InfName
  '    Wscript.Echo "Monitor Name: " &amp; objPrinter.MonitorName
  '    Wscript.Echo "Name: " &amp; objPrinter.Name
  '    Wscript.Echo "OEM Url: " &amp; objPrinter.OEMUrl
  '    Wscript.Echo "Supported Platform: " &amp; objPrinter.SupportedPlatform
  '    Wscript.Echo "Version: " &amp; objPrinter.Version
  
      if InArray(objPrinter.Name, arrPrinters ) = False then
          Wscript.Echo "Name: " &amp; objPrinter.Name
          drivrutinCol.Add    drivrutinCol.Count, Replace(objPrinter.ConfigFile, "C:", "\\" &amp; strComputer &amp; "\c$")
          drivrutinCol.Add    drivrutinCol.Count, Replace(objPrinter.DataFile, "C:", "\\" &amp; strComputer &amp; "\c$")
          drivrutinCol.Add    drivrutinCol.Count, Replace(objPrinter.DriverPath, "C:", "\\" &amp; strComputer &amp; "\c$")
      end if
  
  Next
  
  '   Remove network printers
  Const NETWORK = 22
  Set colInstalledPrinters = objWMIService.ExecQuery _
      ("Select * From Win32_Printer")
  
  For Each objPrinter in colInstalledPrinters 
      If objPrinter.Attributes And NETWORK Then 
  
           ' The code never gets here for user connected network printers
  
      End If
  Next
  
  '   Stop Print Spooler Service
  Set colListOfServices = objWMIService.ExecQuery _
          ("Select * from Win32_Service Where Name ="_
      &amp; strService &amp; " ")
  
  For Each objService in colListOfServices
      objService.StopService()
      WSCript.Sleep intSleep
  Next 
  
  '   Delete drivers
  for i = 0 to drivrutinCol.Count-1
      Wscript.Echo "Deleting driver: " &amp; drivrutinCol.Item(i)
      fsobj.DeleteFile(drivrutinCol.Item(i))
  Next
  
  '   Start Print Spooler Service
  For Each objService in colListOfServices
      WSCript.Sleep intSleep
      objService.StartService()
  Next 
  
  Function InArray(item,myarray)
    Dim i
    For i=0 To UBound(myarray) Step 1
      If InStr(lcase(item), lcase(myarray(i)))&gt;0 Then
        InArray=True
        Exit Function
      End If
    Next
    InArray=False
  End Function
  </code></pre>
  
  <p>The failing part of the code is the "<strong>Remove network printers</strong>" - part. The script does not list the network printers that the user have connected in the user profile, but only the local printers connected to the computer profile. </p>