Script to remove network printers

Removal of printers has always been a problem whether via GPO or otherwise especially migrating across print servers.

https://community.spiceworks.com/scripts/show/318-remove-printers-by-name-or-server makes this a lot easier!

' Remove Printers by Name or Server
' By: Greg Upton March 14th 2010

Dim objNetwork
Set objNetwork = WScript.CreateObject("Wscript.Network")

' *************************** Change Script Below ***************************
' RemovePrinter("\\ServerName\PrinterName")
' RemovePrintersFromServer("ServerName")

' *************************** Change Script Above ***************************

' ######################### Subs 'n Functions Below #########################
' ############################## DO NOT CHANGE ##############################

Set objNetwork = Nothing
WScript.Quit

sub RemovePrinter(sPrinterUNC)
	dim oPrinters
	Set oPrinters = objNetwork.EnumPrinterConnections
	For i = 0 to oPrinters.Count - 1 Step 2
		if uCase(oPrinters.Item(i+1)) = uCase(sPrinterUNC) then 
			on error resume next
			objNetwork.RemovePrinterConnection sPrinterUNC, true, true
			on error goto 0
			exit sub
		end if	
	Next
	set oPrinters = Nothing
end sub

sub RemovePrintersFromServer(sServerName)
	dim oPrinters, aPrinter
	Set oPrinters = objNetwork.EnumPrinterConnections
	For i = 0 to oPrinters.Count - 1 Step 2
		on error resume next
			aPrinter = split(uCase(oPrinters.Item(i+1)),"\",-1, 1)  '"
			if not UBound(aPrinter) = 0 then  'this is not a local printer, process code
				if aPrinter(2) = ucase(sServerName) then
					objNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
				end if
			end if
		aPrinter = ""
		on error goto 0
	Next
end sub

 

Advertisements

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