JSI Tip 7281. What is the current user's default printer?

In tip 7280, I returned the current user's default printer in one of the following formats:



I have scripted DfltPrtType.bat to return the current user's default printer in one of the following formats:

\\<printserver>\Device name

LPTn:Device name

Where Device Name is the hardware description, like one of the following:

HP Business Inkjet 2250 (PCL)

HP OfficeJet R80xi

The syntax for using DfltPrtType.bat is:

call DfltPrtType Printer

where Printer is a call directed environment variable that will contain a string in one of the above formats.

DfltPrtType.bat uses Reg.exe from the Windows 2000 Support Tools, or Reg.exe that is built into Windows XP and later.

If you need to extract the Device Name from the string, the following commands will do it:

if "%Printer:~0,1%" EQU "\" goto netprt
set Printer=%Printer:~5%
goto common
set Printer=%Printer:)=\}%
for /f "Tokens=2 Delims=\" %%p in ('@echo %Printer%') do set Printer=%%p
set Printer=%Printer:\}=)%
DfltPrtType.bat contains:
@echo off
if \{%1\}==\{\} @echo Syntax: DfltPrtType Printer&goto :EOF
set dflt=NONE
set prtn=NONE
for /f "Skip=4 Tokens=2*" %%p in ('REG QUERY "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v Device') do set prtn=%%q
if "%prtn%" EQU "NONE" goto return
set prtn=%prtn:)=\}%
set prtn=%prtn:,=#%
for /f "Tokens=1,3 Delims=#" %%p in ('@echo %prtn%') do set dflt=%%p&set port=%%q
If "%dflt:~0,1%" NEQ "\" goto lcl
set dflt=%dflt:\}=)%
endlocal&set %1=%dflt%
goto :EOF
set dflt=%port:~0,4%:%dflt%
goto return
NOTE: I have tested DfltPrtType.bat with Windows 2000 and Windows XP clients.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.