Tuesday, March 21, 2006

WMI: Retrieving a list of installed printers

This script will retrieve a list of printers installed on a given system. By connecting to the Win32_Printer class - , it builds a collection of everything in the class, and then loops through the collection echoing the strings that we’re interested in (name, location, default).

If you follow the link to the Win32_Printer class, you’ll see that there’s a number of methods we can use to do some work. You could use this snippet as part of a more complex script that helps with some of the preparation work for a print server migration.

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

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

For Each objPrinter in colInstalledPrinters
Wscript.Echo "Name: " & objPrinter.Name
Wscript.Echo "Location: " & objPrinter.Location
Wscript.Echo "Default: " & objPrinter.Default

No comments: