• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

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
0
gymbo731
Asked:
gymbo731
  • 11
  • 9
  • 2
2 Solutions
 
cookreCommented:
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

0
 
gymbo731Author Commented:
Sorry, that didn't work. The printers are still there.
0
 
cookreCommented:
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.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gymbo731Author Commented:
It says that there is an error with line 3 char 1
0
 
gymbo731Author Commented:
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.
0
 
cookreCommented:
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
0
 
gymbo731Author Commented:
OK, that worked. It shows all the names of the printers that are connected. Now I just need them removed.
0
 
gymbo731Author Commented:
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.
0
 
cookreCommented:
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
0
 
gymbo731Author Commented:
Line: 4
Char: 6
Error : Object doesn't support this property or method: 'ptr.local'
Code: 800A01B6
Source: Microsoft VBScript runtime error
0
 
cookreCommented:
Odd, I'll see what I can find ...
0
 
cookreCommented:
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...
0
 
cookreCommented:
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.
0
 
gymbo731Author Commented:
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.
0
 
cookreCommented:
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.
0
 
gymbo731Author Commented:
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.
0
 
cookreCommented:
Will do - I'll post it tomorrow night.
0
 
sr75Commented:
That may have worked but it is the hard way to remove network printers.  This script will do it much easier:

On error Resume Next

Dim objNetwork
Dim strComputer

strComputer="."
Set Net = 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
      Printer = objPrinter.name
      Net.RemovePrinterConnection Printer
next
0
 
cookreCommented:
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.

0
 
gymbo731Author Commented:
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.
0
 
cookreCommented:
Const HKCR=&H80000000
Const HKCU=&H80000001
Const HKLM=&H80000002
Const HKU=&H80000003
Const HKCC=&H80000005


Set WshShell=WScript.CreateObject("WScript.Shell")

' Object for User enumeration
Set oRegU=GetObject("winmgmts:!root/default:StdRegProv")

' Object for printer enumeration
Set oRegP=GetObject("winmgmts:!root/default:StdRegProv")

' Step through users
EnumUsers=oRegU.EnumKey(HKU,"",Users)
if EnumUsers=0 then
   For idxU=0 to UBound(Users)
       ThisUser=Users(idxU)

       ' Step through this user's network printers
       EnumPrinters=oRegP.EnumKey(HKU,ThisUser & "\Printers\Connections",Printers)
       ' Alas, a no hit returns a 0, but unvalued Printers, hence:
       on error resume next
       dummy=UBound(Printers)
       EnumPrinters=err.number
       on error goto 0
       if EnumPrinters=0 then
          for idxP=0 to UBound(Printers)
              ThisPrinter=Printers(idxP)
              DeleteStatus=oReg.DeleteKey(HKCU,ThisPrinter)
              if DeleteStatus<>0 then
                 ' wscript.echo("Delete of " & ThisPrinter & " failed with error code " & DeleteStatus)
                 end if
              next
          end if
      Next
   end if

0
 
sr75Commented:
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 9
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now