JSI Tip 7280. How do I print to a user's default printer in a CMD script?

Jerold Schulman

October 5, 2003

1 Min Read
ITPro Today logo in a gray background | ITPro Today


If you need to print a text-based document to a user's default printer, you need to know what the default is, so you can issue the appropriate command, like one of the following:

print /d:\  print /d:LPT1 

I have scripted DfltPrt.bat to return the logged on user's default printer. The syntax for using DfltPrt.bat is:

call DfltPrt Printer

where Printer is a call directed environment variable that will contain:

N                           - No default printer exists.LPTn                        - Local Printer on port LPTn.\ - Shared network printer.

To print a text-based document, you would use:

print /d:%Printer%

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

DfltPrt.bat contains:

@echo offif {%1}=={} @echo Syntax: DfltPrt Printer&goto :EOFsetlocalset dflt=Nset key="HKCUSoftwareMicrosoftWindows NTCurrentVersionWindows"for /f "Skip=4 Tokens=2*" %%p in ('REG QUERY %key% /v Device') do set prtn=%%qset prtn=%prtn:)=}%set prtn=%prtn:,=#%for /f "Tokens=3 Delims=#" %%p in ('@echo %prtn%') do set ptype=%%pif /i "%ptype:~0,3%" EQU "LPT" goto lclfor /f "Tokens=1 Delims=" %%p in ('@echo %prtn%') do set server=%%pset prtn=%prtn:=%call set prtn=%%prtn:%server%=%%set prtn=%prtn:~0,28%set share=Nset prtn=%prtn:}=)%for /f "Skip=4 Tokens=1*" %%p in ('rmtshare \%server%') do set s1=%%p&set s2=%%q&call :parseif "%share%" NEQ "N" set dflt=\%server%%share%:returnendlocal&set %1=%dflt%goto :EOF:lclset dflt=%ptype:~0,4%goto return:parseset s2=%s2:~0,28%if "%prtn%" EQU "%s2%" set share=%s1%

NOTE: I have tested DfltPrt.bat with Windows 2000 and Windows XP clients.

NOTE: See What is the current user's default printer?



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like