Identifying the Default Printer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Using VBScript or a REGistry extraction, how can i determine the Default
Printer setting (SystemName & Port/connection) for the current User on
Win2000 Pro and WinXP Pro O.S.?
(I know how to enumerate all printers, and set the default printer, but
cannot figure out how to identify the current default printer that a User may
have connected on a Network.)
Thanks for help.
 
Dennis said:
Using VBScript or a REGistry extraction, how can i determine the Default
Printer setting (SystemName & Port/connection) for the current User on
Win2000 Pro and WinXP Pro O.S.?
(I know how to enumerate all printers, and set the default printer, but
cannot figure out how to identify the current default printer that a User may
have connected on a Network.)
Thanks for help.

strComputer = "."
On Error Resume Next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters

' this should do in w2k and Xp
if (objPrinter.Attributes AND 4) = 4 then _
wscript.echo "Default Printer(1) is " & objPrinter.Name
' this requires XP
If objPrinter.Default = True Then _
Wscript.Echo "Default Printer(2) is " & objPrinter.Name
Next

Or for Reg.exe try this
reg query "HKCU\Printers" /v DeviceOld

HTH
 
Found a solution posted by Torgeir Bakken:

With WSH RegRead:

'--------------------8<----------------------
WScript.Echo GetDefaultPrinter

Function GetDefaultPrinter
Set oShell = CreateObject("WScript.Shell")
sRegVal = _
"HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
sDefault = ""
On Error Resume Next
sDefault = oShell.RegRead(sRegVal)
sDefault = Left(sDefault ,InStr(sDefault, ",") - 1)
On Error Goto 0
GetDefaultPrinter = sDefault
End Function
'--------------------8<----------------------


With WMI:

'--------------------8<----------------------
WScript.Echo GetDefaultPrinter

Function GetDefaultPrinter
sComputer = "."
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = oWMIService.ExecQuery("Select * from Win32_Printer",,48)
For Each oItem in colItems
If (oItem.Attributes And 2^(3-1)) = 4 Then
sDefault = oItem.Name
Exit For
End If
Next
GetDefaultPrinter = sDefault
End Function
'--------------------8<----------------------
 
Back
Top