VBScript to remove network printers, when RemovePrinterConnection fails and still have time for popcorn and a movie

For the last couple of months, and some months ahead, I am working primarely on migration projects, currently I have just finished developing a lot of scripts to automatically migrate from Novell & ZenWorks to a purely Microsoft environment. So I might share some tips and tricks from my experiences.

Novell LogoTrying to uninstall Novell printers automatically with VBScripting in a login script gave me a headache, for some reason the good old function WshNetwork.RemovePrinterConnection would fail every time trying to uninstall a Novell printer. After some searching and testing I found that by executing a command to RUNDLL32 PRINTUI.DLL,PrintUIEntry, I could successfully uninstall the printers. My personal experience shows no warnings or errors to the user, as long as the computer can still access Novell, however I did have some warning messages appear when the computer could not connect, but the printer was still deleted, and the user could not abort it, so in my book thats a success!

Also i found some examples removing the printers with /dn instead of the /dl that I am using here, I believe the difference should be if it is a local or network printer, but for me the /dl was the one that worked to removing the Novell printers, You might find different results? let me know what you find out!

Below is the script I created for searching the computer for any printers on a specific print server and remove theese with the RUNDDL command. Since i ran this after allready trying to remove the printers with RemovePrinterConnection, I would only have the printers that failed to remove left. So I didnt need to try that first, but you may? Others might also be able to use this to remove printers from non Novell environments that are resisting removal 🙂

Enjoy!
Oh and ofcourse the user running the script must have administrative priviliges, You should add any logging and error handling you may need, use at own risk and all that…

' You may use this script and parts of it as you please, all that I ask is that you give a small thank you note on my blog www.sole.dk
' Enjoy and use at own risk!

On Error Resume Next ' I dont want to show the user i put a comma in the wrong place!

strSletPrinter = "ServerToRemovePrintersFrom" ' Change this to your server to remove

Set WshShell = WScript.CreateObject("WScript.Shell") ' used for executing a command line command
Set WshNetwork = WScript.CreateObject("WScript.Network") ' used for Networking object control
Set InstalledPrinters = WshNetwork.EnumPrinterConnections ' used for getting current connected printers

WScript.Echo "Starting auto removal of Novell printers."
' Check for any printers that needs deletion from the old servers var
' fix variable with printers to delete for each installed printer

WScript.Echo "Listing installed printers on computer"
For Each printer In InstalledPrinters
    WriteLog "Printer - " & printer
    ' if installed printer is a network printer, if you want to remove all printers just remove the next two if statements and corresponding end if..
    If InStr(printer, "\\") Then
        arrPrinter = Split(printer,"\") ' I only want the servername part
        If LCase(strSletPrinter) = LCase(arrPrinter(2)) Then
            RunLine = "RUNDLL32 PRINTUI.DLL,PrintUIEntry /dl /n\\"&arrPrinter(2)&"\"&arrPrinter(3)
'            WshNetwork.RemovePrinterConnection "\\" & arrPrinter(2) & "\" & arrPrinter(3)
             ' uncomment the line above to remove any type of printer
            WshShell.Run strRunLine, 0, True  ' 0 minimized and True wait for completion
            WScript.Echo "removed printer : " & "\\" & arrPrinter(2) & "\" & arrPrinter(3)
            ' you can make your own error handling here with If Err.Number <> 0 Then ...
        End If
    End If
Next

Set InstalledPrinters = Nothing
Set WshNetwork = Nothing
Set WshShell = Nothing

9 Responses to “VBScript to remove network printers, when RemovePrinterConnection fails and still have time for popcorn and a movie”

  • Sole:

    I still had a few printers, that just would not get uninstalled. After some research i figured out that the printers that would still not get uninstalled, had jobs waiting in the spooler queue, but since the server was no longer accessible, the jobs would never complete. I opted for a simple solution and added to the script the following.
    If fail on vbs and printui.dll method, do the following:

    stop spooler service
    delete all files in spooler directory
    start spooler service
    retry printui.dll removal method

    This removed all the last remains of the printers. Hope it helps someone else, if You need the actual finished script, let me know.

  • Hi,

    Just starting our own migration from Novell to AD and can see that your scripts could come in very handy. (about 2000 computers) is it possible to get a copy of your complete script ?

  • Sole:

    The “complete” script has been customized heavily for each customers migration project. However if you give me some more information on what you plan to do during the migration and how you will execute the script on the machines, I can give You some different snippets to use. Ill send You an e-mail as well for contact.

  • Ralf Scholl:

    Thank you very much for your BLOG.

    I never thought about using the /dl option instead of /dn to delete an IPP printer on network.

    Regards
    Ralf

  • Sole:

    That was by trial and error I found that (dl is usually for local and dn for network, but in this case it appears as local even if its a network printer)

  • Jesse:

    I have tried using this script and keep coming accross an error at line 24 carachter 50.
    “WshShell.Run(strRunLine, 0, True) ‘ 0 minimized and True wait for completion” that part the error is “Cannot use pareentheses when calling a sub” bassicaly i have only a little amount of programming knowledge when it comes to VB and i cannot fix it! can you help?

  • Sole:

    Nice catch, the error is due to the command having its parameters in ( ), when getting the output of the command, that is needed. In the original script i did get the output, but not in this simpler version. Just removed the () and it should work for You now.

    Try it again now.

  • Jesse:

    I have tried this again. It says that it has removed about half of the list in Printers and Faxes but even after a restart these are still there. I have removed the IF statements and End if.

  • Sole:

    This script is specific for removing Novell printers, but if you just want it to remove standard printers, try adding the following line, just before “runline = ..”

    WshNetwork.RemovePrinterConnection “\\” & arrPrinter(2) & “\” & arrPrinter(3)

Leave a Reply