Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove all network printers except default 2

Status
Not open for further replies.

Cain8869

MIS
Feb 22, 2008
3
US
I would like to be able to remove all of the network printers from a computer except for the default one. Here is a snipit of the code I am using that removes all networked printers (lifted from markdmac's faq. thank you!!! you're a lifesaver):

Code:
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
(LOOP_COUNTER +1),True,True
Next
Thanks for your help in advance!
 
Code:
Option Explicit

Dim strComputer : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & _
						strComputer & "\root\cimv2")
Dim colPrinters : Set colPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer WHERE Default=True")
Dim objPrinter
For Each objPrinter In colPrinters
	WScript.Echo objPrinter.DeviceID
Next

Look at what this gives you...put it into a variable...do not remove if it is your default.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
dm4ever,
Thank you for your speedy reply!

You code works quite well in returning the default printer, but I was wondering if you could answer a noob question.

I see how colPrinters is collecting printer information, but what I do not understand is how is objPrinter getting passed the information within colPrinters? i dont think i am running the right syntax for this command as it is not eventually setting the default name:

Code:
WshNetwork.SetDefaultPrinter(objPrinter.DeviceID)

Please forgive my being wet behind the ears. This is my first time attempting to script.
 
Please post the code you are trying so we can help you correct it to accomplish what you are after.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Give this a try
Code:
'==========================================================================
'
' NAME: DeleteNetworkPrintersExcludeDefault.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 2/25/2008
' COPYRIGHT (c) 2008 All Rights Reserved
'
' COMMENT: 
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================
On Error Resume Next
Dim strComputer, objWMIService,colPrinters,objPrinter,WSHNetwork
Set WSHNetwork = WScript.CreateObject("WScript.Network")

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

StopService ("Spooler")
WScript.Sleep 3000

Set colPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer WHERE Default<>True")
For Each objPrinter In colPrinters
    If Left(objPrinter.PortName,2) = "\\" Then 
	Wscript.echo objPrinter.Name
        Wscript.echo objPrinter.PortName
     WSHNetwork.RemovePrinterConnection objPrinter.PortName,True,True
    End If
Next



StartService ("Spooler")

'sub to stop the services
Sub StopService (ServiceName)
         queryString = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
         set results = objWMIService.execquery(queryString)
         for each service in results
            if service.state = "Running" then
             service.stopService
            end if
          next
End Sub

'sub to start the services
Sub StartService (ServiceName)
         queryString = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
         set results = objWMIService.execquery(queryString)
         for each service in results
            if service.state <> "Running" then
             service.startService
            end if
          next
End Sub

If you are wondering why I am stopping the spooler service it is because in testing this script I found that the printers can be locked and prevent removal. Stopping the service allows them to be deleted. Note that this will of course wipe out any print jobs in the queue. That should not be a problem if you are using this as part of your login script.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I hope you understand what has been provided...what Mark did is change the WMI query to select any printer that isn't set as the Default....you can further modify the script so you don't need the If...Then statement in the For...Next loop

Code:
Option Explicit

Dim strComputer : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & _
                        strComputer & "\root\cimv2")
Dim colPrinters : Set colPrinters = objWMIService.ExecQuery(_
				  "SELECT * FROM Win32_Printer WHERE Default<>True AND DeviceID LIKE '\\%'")
Dim objPrinter
For Each objPrinter In colPrinters
    WScript.Echo objPrinter.DeviceID
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Oh one more thing....Mark's method is safer if you have Win2k boxes or anything older since using LIKE only works on WinXP or better if I remember correctly.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Awesome. Markdmac, your script worked well, but I ended up modifying Dm4ever's snipit. Here is what I ended up with:

Code:
'Remove ALL old printers except Default
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & _
                        strComputer & "\root\cimv2")
Dim colPrinters : Set colPrinters = objWMIService.ExecQuery(_
                  "SELECT * FROM Win32_Printer WHERE Default<>True AND DeviceID LIKE '\\%'")
Dim objPrinter
For Each objPrinter In colPrinters
    WSHNetwork.RemovePrinterConnection objPrinter.DeviceID
Next

This worked flawlessly since I am not burdend with any computers older then XP. Thank you both for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top