views:

585

answers:

2

Using the Wscript.Network object shown below, is there an easy way to retrieve the default printer on a machine? I know how to set the 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 don't want to use WMI for that reason.

Set objNetwork = CreateObject("WScript.Network") 
Set objLocalPrinters = objNetwork.EnumPrinterConnections
+1  A: 

From (MSDN):

The EnumPrinterConnections method returns a collection. This collection is an array that associates pairs of items — network printer local names and their associated UNC names. Even-numbered items in the collection represent printer ports. Odd-numbered items represent the networked printer UNC names. The first item in the collection is at index zero (0).

So there is little chance to get the default printer from this collection. Sorry

Greetz, GHad

GHad
A: 

The WshNetwork.EnumPrinterConnections collection doesn't provide any information about the default printer. You can try retrieving the default printer name from the registry instead, though I'm not sure if it's reliable:

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


As for WMI, it's true that some WMI classes and class members aren't available on older Windows versions. For example, the Win32_Printer.Default property that indicates whether the printer is the default one, doesn't exist on Windows 2000/NT. Nevertheless, there's a simple workaround for finding the default printer on those Windows versions, which consists in checking for the PRINTER_ATTRIBUTE_DEFAULT attribute in each printer's Attribute bitmask:

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

This code works on later Windows versions as well.

For details, check out this Hey, Scripting Guy! article: Is There Any Way to Determine the Default Printer On a Computer?

Helen
I saw a post similar to this as well, but it doesn't seem to be relevant to Windows 7.
Mark