Bewerken

Delen via


WMI Tasks: Printers and Printing

WMI tasks for printers and printing manage and obtain data about printers, such as finding or setting the default printer. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

To run a script

  1. Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript filename.vbs at the command prompt.
  4. If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).

Note

By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I... WMI classes or methods
...add a new printer connection to a remote computer? Use the Win32_Printer class and the AddPrinterConnection method.
VB
strComputer = "atl-ws-01"
Set objWMIService = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
Set objPrinter = objWMIService.Get("Win32_Printer")
errReturn = objPrinter.AddPrinterConnection ("\\PrintServer1\ArtDepartmentPrinter")
...set the default printer?

Use the Win32_Printer class and the SetDefaultPrinter method.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = 'ScriptedPrinter'")
For Each objPrinter in colInstalledPrinters
    objPrinter.SetDefaultPrinter()
Next
PowerShell
$printerName = "\\ServerName\ShareName"
$printer = get-wmiObject -class win32_printer -Namespace $namespace | Where-Object { $_.Name -eq $printerName }
[void]$printer.setDefaultPrinter()
...cancel print jobs using WMI?

Use the Win32_Printer class, and the CancelAllJobs method.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrintJobs =  objWMIService.ExecQuery ("Select * from Win32_Printer")
For Each objPrintJob in colPrintJobs 
    objPrintJob.CancelAllJobs
Next
PowerShell
$result = (get-wmiObject -class win32_printer -Namespace "root\cimv2").CancelAllJobs()
...determine the default printer for a computer?

Use the Win32_Printer class, and check whether the Default property is True.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer Where Default = True")
For Each objPrinter in colInstalledPrinters
    Wscript.Echo objPrinter.Name
Next
PowerShell
get-wmiObject -class win32_printer -Namespace "root\cimv2" | where-object { $_.Default -eq 'True' }

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter