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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select multiple printers

Status
Not open for further replies.

kadara

Programmer
Mar 2, 2012
26
0
0
DE
Hi,
I would like to select all printers installed on the computer that contains a specific name (for example: select all printers with "Microsoft" in the name of the printer).
I used the following code, but doesn't work:

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'Microsoft*'")

For Each objPrinter in colInstalledPrinters
    Wscript.Echo "Name: " & objPrinter.Name
    'Wscript.Echo "Location: " & objPrinter.Location
    'Wscript.Echo "Default: " & objPrinter.Default
Next
 
I'd try this:
Where Name Like '%Microsoft%'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
kadara:
Strange... I tested PHV's suggestion on my own Windows 7 box, and it worked.

If you use the line below, are you sure one of the printers actually has 'Microsoft' in the name?
Code:
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
 
I tried PHV's suggestion on my Windows 7 x64 box and it did not return my XPS printer. This slight modification did work for me.

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")

For Each objPrinter in colInstalledPrinters
	If InStr(LCase(objPrinter.Name),"microsoft") > 0 Then
    Wscript.Echo "Name: " & objPrinter.Name
    'Wscript.Echo "Location: " & objPrinter.Location
    'Wscript.Echo "Default: " & objPrinter.Default
    End If
Next

I hope that helps.

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.
 
Mark, I"ve got the XPS printer on my Win7 x64 box with this WQL:
Set colInstalledPrinters = objWMIService.ExecQuery _
[tab]("Select * from Win32_Printer WHERE Name Like '%Microsoft%'")
 
Hi PHV. My apologies, after closer review I see I only changed the name Microsoft* to %Microsoft% and did not replace the = with Like. I bet that is what kadara did too.
Your solution works perfectly.


I hope that helps.

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.
 
Ditto PHV, I have Win7 x64 as well. Are you sure you were using the 'LIKE' operator?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top