Link to home
Start Free TrialLog in
Avatar of Headmasters
HeadmastersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Printer Removal Script

I need to remove all network printers from everyone's machine. I know you can script a script to add network printers. Is there a way that you can write a script on log off to remove all network printers?
Avatar of michko
michko
Flag of United States of America image

This script includes a section to disconnect network printers:
http://www.tek-tips.com/faqs.cfm?fid=5798
Avatar of Headmasters

ASKER

Im using as suggested

ON ERROR RESUME NEXT
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item
(LOOP_COUNTER +1),True,True
End If
wscript.quit

However i keep getting a Scrip Error saying Expected statement. Code 800A0400, Source Compilation error
hmuser - sorry, don't know what is causing that error.  I've posted another questions asking for review of this script here:  https://www.experts-exchange.com/questions/23005909/Need-someone-to-review-a-script-to-delete-all-network-printers-error-pointer-to-another-question.html

We'll pick up another expert who can straighten it out for you.
I think that the step 2 gives the error (but I'm not sure)
So can you try this:




ON ERROR RESUME NEXT
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item
(LOOP_COUNTER +1),True,True
End If
wscript.quit

Open in new window

I have tried that but it comes back with expected statement.

Line 6
Char 1
Error Expected Statement
Code 800A0400
Source Compilation error
Do you have the variable WSHNetwork declared?
See line 2 how to declare it?

ON ERROR RESUME NEXT
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item
(LOOP_COUNTER +1),True,True
End If
wscript.quit

Open in new window

and also forgot (must learn to read everything.....)
add Next in the script.
ON ERROR RESUME NEXT
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 
 If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
  WSHNetwork.RemovePrinterConnection WSHPrinters.Item
  (LOOP_COUNTER +1),True,True
 End If
next
wscript.quit

Open in new window

Im new to the scripting world sorry

Error now is Line 7
Char 3
Expected statement
800A0400
Compilation
it's nothing...
try this:
ON ERROR RESUME NEXT
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 step 2
 If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
  WSHNetwork.RemovePrinterConnection 
  WSHPrinters.Item(LOOP_COUNTER +1),True,True
 End If
next
wscript.quit

Open in new window

If the error is in the line 7, you need to delete some breaklines:

  WSHNetwork.RemovePrinterConnection WSHPrinters.Item
  (LOOP_COUNTER +1),True,True

Comes to be:
  WSHNetwork.RemovePrinterConnection WSHPrinters.Item, (LOOP_COUNTER +1),True,True
It now runs with no errors but the network printers arent removed from the printers and faxes.
ASKER CERTIFIED SOLUTION
Avatar of avatar-e
avatar-e
Flag of Chile image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of venema
venema

All solutions above did not work for me.
The only solution which worked was:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
      ("Select * From Win32_Printer Where Network = True")
For Each objPrinter in colInstalledPrinters
      objPrinter.Delete_
Next