VBS - Get Default Printer

Using the Wscript.Network object shown below, is there an easy way to get the default printer on the machine? I know how to set a default printer, but I'm looking to get the current default printer name. I have a mixture of Windows 2000, XP, and 7 clients and do not want to use WMI for this reason.

Set objNetwork = CreateObject("WScript.Network") 
Set objLocalPrinters = objNetwork.EnumPrinterConnections
+5
source share
2 answers

The collection WshNetwork.EnumPrinterConnectionsdoes not provide any default printer information. You can try to get the default printer name from the registry, although I'm not sure how reliable it is:

Set oShell = CreateObject("WScript.Shell")
strValue = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
strPrinter = oShell.RegRead(strValue)
strPrinter = Split(strPrinter, ",")(0)
WScript.Echo strPrinter


WMI, , WMI Windows. , Win32_Printer.Default, , , Windows 2000/NT. , Windows, PRINTER_ATTRIBUTE_DEFAULT Attribute:

Const ATTR_DEFAULT = 4
strComputer = "."

Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = oWMI.ExecQuery("SELECT * FROM Win32_Printer")

For Each oPrinter in colPrinters
    If oPrinter.Attributes And ATTR_DEFAULT Then 
        Wscript.Echo oPrinter.ShareName
    End If
Next

Windows.

. , , ! : ?

+3

(MSDN):

EnumPrinterConnections . , - UNC. . UNC-. (0).

, .

Greetz, GHAD

+2

All Articles