?
Solved

Deleting printers using a VBS Script

Posted on 2005-03-15
22
Medium Priority
?
524 Views
Last Modified: 2008-01-09
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
Comment
Question by:gymbo731
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
  • 2
22 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 13551042
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
 

Author Comment

by:gymbo731
ID: 13570201
Sorry, that didn't work. The printers are still there.
0
 
LVL 22

Expert Comment

by:cookre
ID: 13570394
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!

 

Author Comment

by:gymbo731
ID: 13570440
It says that there is an error with line 3 char 1
0
 

Author Comment

by:gymbo731
ID: 13570488
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
 
LVL 22

Expert Comment

by:cookre
ID: 13570564
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
 

Author Comment

by:gymbo731
ID: 13570599
OK, that worked. It shows all the names of the printers that are connected. Now I just need them removed.
0
 

Author Comment

by:gymbo731
ID: 13570680
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
 
LVL 22

Expert Comment

by:cookre
ID: 13570723
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
 

Author Comment

by:gymbo731
ID: 13570749
Line: 4
Char: 6
Error : Object doesn't support this property or method: 'ptr.local'
Code: 800A01B6
Source: Microsoft VBScript runtime error
0
 
LVL 22

Expert Comment

by:cookre
ID: 13571277
Odd, I'll see what I can find ...
0
 
LVL 22

Expert Comment

by:cookre
ID: 13571483
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
 
LVL 22

Expert Comment

by:cookre
ID: 13571820
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
 

Author Comment

by:gymbo731
ID: 13578278
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
 
LVL 22

Expert Comment

by:cookre
ID: 13586099
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
 

Author Comment

by:gymbo731
ID: 13605327
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
 
LVL 22

Expert Comment

by:cookre
ID: 13606961
Will do - I'll post it tomorrow night.
0
 
LVL 15

Accepted Solution

by:
sr75 earned 200 total points
ID: 13608598
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
 
LVL 22

Expert Comment

by:cookre
ID: 13608692
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
 

Author Comment

by:gymbo731
ID: 13616455
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
 
LVL 22

Assisted Solution

by:cookre
cookre earned 200 total points
ID: 13618171
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
 
LVL 15

Expert Comment

by:sr75
ID: 13622217
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question