General discussion

Locked

How to remove network printers via a VBS script?

By Computer Dude ·
Hi all,
I need to create a VBS logon script for users here to remove network printer conncections if they exist for a user's AD account. I've figured out how to add network printer connections, but I've run into a brick wall with removing them. I'd like the script to remove printer connections IF they exist, if not I don't want my users to get a pop up message stating that the connection does not exist. Any help would be awesome...

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by pryzhkov In reply to How to remove network pri ...

Solution 1:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
For i = 0 to oPrinters.Count -1
WScript.Echo oPrinters.Item(i)
if InStr(objPrinter.Name, "\\") > 0 then
WshNetwork.RemovePrinterConnection oPrinters.Item(i)
end if

Next

Solution 2:

strComputer = "."
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
Wscript.Echo "Name: " & objPrinter.Name
Wscript.Echo "Location: " & objPrinter.Location
if InStr(objPrinter.Name, "\\") > 0 then
wscript.echo "Will delete: " & objPrinter.Name
' WshNetwork.RemovePrinterConnection objPrinter.Name
end if
Next

I personally prefer #2 cause #1, by some strange reason, enumerates printers twice, by alias and by name... It is not a problem, just not as beautiful as #2.

Collapse -

scripts no longer work

by jtek In reply to

Myself and others have found the scripts listed above no longer work. They no longer return the name of printers where the status is unable to connect ie printer server down or replaced.

Collapse -

The script is working

by mbourgeois In reply to scripts no longer work

The script returns the name of the printer and not the "share name" which is probably what you are looking for. I am using this script and it works like a charm. Try replacing "objPrinter.Name" with "objPrinter.shareName" and you should have what you are looking for.

Collapse -

by sean In reply to

So after much effort I managed to find bits and pieces that helped me create this one, its not as short as the others, but I know this works - use it if you want, just let me know if there are any issues please:

On error Resume Next

Dim objNetwork
Dim strUNCPrinter1
Dim strUNCPrinter2
Dim strUNCPrinter3
Dim strUNCPrinter4
Dim strUNCPrinter5
Dim strUNCPrinter6
Dim strUNCPrinter7
Dim strUNCPrinter8
Dim strUNCPrinter9
Dim strUNCPrinter10


strUNCPrinter1 = "\\<server>\Printer1"
strUNCPrinter2 = "\\<server>\Printer2"
strUNCPrinter3 = "\\<server>\Printer3"
strUNCPrinter4 = "\\<server>\Printer4"
strUNCPrinter5 = "\\<server>\Printer5"
strUNCPrinter6 = "\\<server>\Printer6"
strUNCPrinter7 = "\\<server>\Printer7"
strUNCPrinter8 = "\\<server>\Printer8"
strUNCPrinter9 = "\\<server>\Printer9"
strUNCPrinter10 = "\\<server>\Printer10"


Set objNetwork = CreateObject("WScript.Network")


objNetwork.RemovePrinterConnection strUNCPrinter1
objNetwork.RemovePrinterConnection strUNCPrinter2
objNetwork.RemovePrinterConnection strUNCPrinter3
objNetwork.RemovePrinterConnection strUNCPrinter4
objNetwork.RemovePrinterConnection strUNCPrinter5
objNetwork.RemovePrinterConnection strUNCPrinter6
objNetwork.RemovePrinterConnection strUNCPrinter7
objNetwork.RemovePrinterConnection strUNCPrinter8
objNetwork.RemovePrinterConnection strUNCPrinter9
objNetwork.RemovePrinterConnection strUNCPrinter10


Wscript.Quit

Collapse -

by jdaly In reply to How to remove network pri ...

On error Resume Next
Dim cNetCommands, vPrinterName, vPrinterPath
Set cNetCommands = WScript.CreateObject("WScript.Network")
Set vPrinterPath = cNetCommands.EnumPrinterConnections
For Each vPrinterName in vPrinterPath
cNetCommands.RemovePrinterConnection(vPrinterName)
Next
Wscript.Quit

Much simpler, works just as written above.

Collapse -

Default printer?

by mhartmann In reply to

Dear Jdaly - this script works just great.

Is there any chance to
Step1: read the current default printer, and store that information
Step2: run your script to the clear the printer list and
Step3: install & write back the default printer that was pulled before?

That would be awesome :) Thank you!

Back to Desktop Forum
6 total posts (Page 1 of 1)  

Related Discussions

Related Forums