Posted by: David | July 9, 2011

Script to Remove all network printers including those installed locally with TCP/IP Ports

I was recently asked if it was possible to remove all printers from a Workstation that were networked via a script. I figured this would be a fairly simple thing to do.

There are a number of ways you can interface with Printers from VBS but in the end I found the most successful to be through WMI, the script below can be run remotely by putting a computer name in the value strcomputer = “.” line.

The script will enumerate all printers on a computer and then if the printer is marked a networked by the OS (i.e. Installed from a printer share \PRINTSERVERPrinter1) it will be removed. The script will also look at the port a printer is set up on, if is the port is a TCPIP port point to an IP address or a WSD port it will also remove these printers.

The Code:

' Title: Remove all Networked printers
' Last Updated: 28/06/2011
' Written By: David Gardner
' Version: 1.0
' Description: VBS Script to Delete all network printers
' This includes printers installed locally with a TCP/IP or WSD port
' tested on Windows XP and Windows 7

'Set to run on local machine
strComputer = "."

'Create WMI Object
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")

'Get all printers installed on Computer
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")

'Loop through printers and delete network ones
For Each objPrinter in colInstalledPrinters
if (objPrinter.network) or (checkfornetwork(objPrinter.PortName)) then
objPrinter.Delete_
end if
Next

'Function to check if printer is a network printer by looking at Printer port properties
function checkfornetwork(printerport)
splitport = split(printerport,".")
if (ubound(splitport) = 3) then
if (splitport(0)*1 < 255) and (splitport(1)*1< 255 ) and (splitport(2)*1 < 255 ) and (splitport(3)*1 < 255) then
checkfornetwork = TRUE
end if
elseif (ucase(left(printerport,3)) = "WSD") then
checkfornetwork = TRUE
else
checkfornetwork = FALSE
end if
end function

Advertisements

Responses

  1. how can you run the code on windows 7?can you help me with the steps

    • How are you trying to run the code? as part of a login script for a number of computers or on just one machine?

  2. I am trying to run this script on one machine. Later , I would like to push this out remotely to many workstations. I keep getting an error that reads
    Line 22 Char 9
    Type mismatch ‘Splitport(…)’

    Please help,
    Bender

    • Hi Steve,

      That looks like you might have a syntax error, could you confirm what OS you are trying to run the script on and how you are calling it?

      Thanks

      David

  3. This is on Windows XP
    CScript “c:\PrinterRemove.vbs”

  4. I added something to your script and thought it might be helpful for everyone else.
    Your script doesn’t take into account ports that are named differently than the ip address. I added a section to see if the portname matches a port name with port 9100. This works for us since all our printers are HP.

    Here is the modified code.

    'Title: Remove all Networked printers
    ' Last Updated: 28/06/2011
    ' Written By: David Gardner
    ' Version: 1.0
    ' Description: VBS Script to Delete all network printers
    ' This includes printers installed locally with a TCP/IP or WSD port
    ' tested on Windows XP and Windows 7

    'Set to run on local machine
    strComputer = "."

    'Create WMI Object
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")

    'Get all printers installed on Computer
    Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * from Win32_Printer")

    'Loop through printers and delete network ones
    For Each objPrinter in colInstalledPrinters

    if (objPrinter.network) or (checkfornetwork(objPrinter.PortName)) or (check9100(objPrinter.PortName)) then

    objPrinter.Delete_

    Wscript.Echo "Deleted: " & objPrinter.PortName
    end if
    Next

    function check9100(printerport)

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_TCPIPPrinterPort where PortNumber=9100",,48)

    For Each objItem in colItems

    if (objItem.Name = printerport) then
    check = 1
    end if
    Next
    if (check = 1) then
    check9100 = TRUE
    else
    check9100 = FALSE
    end if

    end function

    'Function to check if printer is a network printer by looking at Printer port properties

    function checkfornetwork(printerport)
    splitport = split(printerport,".")
    if (ubound(splitport) = 3) then
    if (splitport(0)*1 < 255) and (splitport(1)*1< 255 ) and (splitport(2)*1 < 255 ) and (splitport(3)*1 < 255) then
    checkfornetwork = TRUE
    end if
    elseif (ucase(left(printerport,3)) = "WSD") then
    checkfornetwork = TRUE
    else
    checkfornetwork = FALSE
    end if
    end function

  5. Thanks for this script David. It’s exactly what I’m looking for.
    However, I’m getting the same error as Steve/Bender:

    Line: 31
    Char: 9
    Error: Type Mismatch: ‘splitport(…)’
    Code: 800A000D

    My scripting knowledge is very limited at best.
    Any assistance would be very much appreciated.

    Cheers,
    Kevin

  6. Hi All,

    I tested on windows 7 & an XP Pro box and it worked fine, but then I sent to a user to “test” and they advise and sent screenshot of exact same error, but they are also on an XP Pro machine so it doesnt seem consistant…….

    Line 22 Char 9
    Type mismatch ‘Splitport(…)’

    any updates to remove this error would be great 🙂

  7. I realize this is an old thread but for those who find this in a Google search results like I did…

    If you’re having issues running this on Windows XP and getting the Splitport error it might be because Windows XP labels TCP/IP printer ports with an “IP_” first, so IP_192.168.XXX.XXX for example. A quick fix is to add the following line before splitport = split(printerport,”.”):

    printerport = replace(printerport, “IP_”, “”)

  8. I took Jims suggestion from Jan 17 2013 and added the printerport = replace(printerport, “IP_”, “”)
    line before the splitport = split(prinerport,”.”)
    I am still getting the Error: Type mismatch:’splitport(…)’
    Code: 800A000D
    Any other Suggestions??
    Thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: