Link to home
Start Free TrialLog in
Avatar of gymbo731
gymbo731

asked on

Deleting printers using a VBS Script

Hello,

I need a VBS script that will remove all the network printers from my computer. I am running windows XP and windows 2000 SP4 on my machines. I have printers shared on some computers. The problem is that some computers have way to many printers installed on them. I need a script to run on those computers so that it can delete the network printers that are connected to it for all users on that computer. I am running a domain with active directory, and roaming profiles.

I have a script that can create the printers I need, but I need to have a script that would delete all network printers. Please help if you can.

Thanks,
Jim
Avatar of cookre
cookre
Flag of United States of America image

Set obj=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set netptrs =  obj.ExecQuery("Select * from Win32_Printer Where Network = true'")
For Each ptr in netptrs
    ptr.Delete
   next

Avatar of gymbo731
gymbo731

ASKER

Sorry, that didn't work. The printers are still there.
A wee typo, I left out a single-quote (') just in front of the "true".
The line should have been:

Set netptrs =  obj.ExecQuery("Select * from Win32_Printer Where Network = 'true'")

To test it out before actually deleting, replace:

ptr.delete

with wscript.echo(ptr.name)

to make sure the correct printers are identified.


It says that there is an error with line 3 char 1
If I can't get this to work with the deleting of the network printers that are connected to it, do you know a script where if there is already a printer installed on the computer (local or network) it will add the existing printer to every user that logs into that computer?

Thanks for all the help.
If it said in the error display:

...
Source:    (null)

that means it didn't find any such printers.


Run this one to just list the names of the printers

Set obj=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set netptrs =  obj.ExecQuery("Select * from Win32_Printer")
For each ptr in netptrs
     wscript.echo ptr.name
     next
OK, that worked. It shows all the names of the printers that are connected. Now I just need them removed.
It can see the printers, but it can't remove them. WHY?? I am logged in as administrator. I ran the delete script you gave me and the source still says (null)

Thanks again for all the help. I really apprichiate it.
Now let's identify where it thinks the printers are:

Set obj=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set netptrs =  obj.ExecQuery("Select * from Win32_Printer")
For each ptr in netptrs
     wscript.echo ptr.name & "  Local:" & ptr.local & "  Network:" & ptr.network
     next
Line: 4
Char: 6
Error : Object doesn't support this property or method: 'ptr.local'
Code: 800A01B6
Source: Microsoft VBScript runtime error
Odd, I'll see what I can find ...
Well poo.  Reading the not so fine print in the documentation for both .local and .network yielded the follow unwanted comment for both:

Windows 2000 and Windows NT 4.0:  This property is not supported.

It's starting to sound like you'll need a Registry hack.  I'll see what I can find...
It LOOKs like deleting the subkeys of

HKEY_CURRENT_USER\Printers\Connections

is sufficient to delete network printers (local printers being stored elswhere).


Not being the adventurous type, I'm reluctant to try it.  If you're willing to, and it seems to work, we can come up with a vbscript to step through HKEY_USERS and delete their network printers.
Sure, it would be nice to have a script that would be able to delete those registry keys for me. It would be a pain to hit every machine and remove the printers manually.
This removes them from HKCU:

Const HKCR=&H80000000
Const HKCU=&H80000001
Const HKLM=&H80000002
Const HKU=&H80000003
Const HKCC=&H80000005

Set WshShell=WScript.CreateObject("WScript.Shell")
Set oReg=GetObject("winmgmts:!root/default:StdRegProv")
EnumStatus=oReg.EnumKey(HKCU,"Printers\Connections",NetPrinters)
if EnumStatus=0 then
   For idx=0 to UBound(NetPrinters)
       ThisPrinter="Printers\Connections\" & NetPrinters(idx)
       DeleteStatus=oReg.DeleteKey(HKCU,ThisPrinter)
       if DeleteStatus<>0 then
          wscript.echo("Delete of " & ThisPrinter & " failed with error code " & DeleteStatus
          end if
       Next
else
   wscript.echo "No network printers found"
   end if


If it works for you, we'll expand it to step through all users on the box.
There was an error:

Line: 15
Char: 94
Error: Expected ")"

But I found it and fixed it. Anyways, yes it did delete all the network printers. Please expand it to step through all users.

Thanks.
Will do - I'll post it tomorrow night.
ASKER CERTIFIED SOLUTION
Avatar of sr75
sr75
Flag of United States of America 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
Yup, that's cleaner - if local printers will always be named LPTn so they can be filtered out.

Although the short description of RemovePrinterConnection says:
"Removes a shared network printer connection from your computer system",
the Arguments and Remarks details say it will also remove local printers, hence the need to identify local printers by name.

That one works pretty good. I will see how the cookre script works though before I accept an answer.

I just have another question, is there an add printer login script that will make the default printer the one in that room? Like right now I have a printer script that will add all the printers I need to the user, but I would like to have a script so that when they log into a certain room, the printer in that room changes to their default printer.

Here is an example:
Say I have two printers shared in different rooms. The computers in the one room are named CAD-01 through CAD-20. The Computers in the other room are named Clab-01 through Clab-25. The printer in the CAD room is named "CAD room printer", and the printer in the CLab is named "Clab printer" If I log into a computer in the CAD room, I would like the script to change my default printer to the "CAD room printer", and if then I go to the Clab room, I would like my default printer to change to the "Clab printer"

Some people just don't know how to find the right printer when they are printing, or they just hit the print button on the top from Word. So this script would be awesome!!!

I hope this all makes sense. Thank you sooooo much for the help so far. You have been a life saver. I am thinking a might even increase the points so you could get more.

Thanks.
SOLUTION
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
Yes, do you have a difference between the computer naming conventions or maybe subnetted differently between the rooms?  If not you will need to generate a file for each room or seperate the AD computer accounts into different groups for each room Then have it read and do a if instr(stream, compname) then arguement.  It should look something like this:

Set Net = CreateObject("WScript.Network")

if ************** then
                Net.SetDefaultPrinter "\\server\share"
end

if ************** then
                Net.SetDefaultPrinter "\\server\share"
end


the if instr() expression says if compname is in file/group stream then do the following.