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
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
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.
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.
ASKER
It says that there is an error with line 3 char 1
ASKER
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.
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:{i mpersonati onLevel=im personate} !\\.\root\ cimv2")
Set netptrs = obj.ExecQuery("Select * from Win32_Printer")
For each ptr in netptrs
wscript.echo ptr.name
next
...
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:{i
Set netptrs = obj.ExecQuery("Select * from Win32_Printer")
For each ptr in netptrs
wscript.echo ptr.name
next
ASKER
OK, that worked. It shows all the names of the printers that are connected. Now I just need them removed.
ASKER
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.
Thanks again for all the help. I really apprichiate it.
Now let's identify where it thinks the printers are:
Set obj=GetObject("winmgmts:{i mpersonati onLevel=im personate} !\\.\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
Set obj=GetObject("winmgmts:{i
Set netptrs = obj.ExecQuery("Select * from Win32_Printer")
For each ptr in netptrs
wscript.echo ptr.name & " Local:" & ptr.local & " Network:" & ptr.network
next
ASKER
Line: 4
Char: 6
Error : Object doesn't support this property or method: 'ptr.local'
Code: 800A01B6
Source: Microsoft VBScript runtime error
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...
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 \Connectio ns
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.
HKEY_CURRENT_USER\Printers
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.
ASKER
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.CreateObj ect("WScri pt.Shell")
Set oReg=GetObject("winmgmts:! root/defau lt:StdRegP rov")
EnumStatus=oReg.EnumKey(HK CU,"Printe rs\Connect ions",NetP rinters)
if EnumStatus=0 then
For idx=0 to UBound(NetPrinters)
ThisPrinter="Printers\Conn ections\" & NetPrinters(idx)
DeleteStatus=oReg.DeleteKe y(HKCU,Thi sPrinter)
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.
Const HKCR=&H80000000
Const HKCU=&H80000001
Const HKLM=&H80000002
Const HKU=&H80000003
Const HKCC=&H80000005
Set WshShell=WScript.CreateObj
Set oReg=GetObject("winmgmts:!
EnumStatus=oReg.EnumKey(HK
if EnumStatus=0 then
For idx=0 to UBound(NetPrinters)
ThisPrinter="Printers\Conn
DeleteStatus=oReg.DeleteKe
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.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Netw ork")
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.
Set Net = CreateObject("WScript.Netw
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.
Set netptrs = obj.ExecQuery("Select * from Win32_Printer Where Network = true'")
For Each ptr in netptrs
ptr.Delete
next