Q. How can a script set an environment variable to the logged on users default printer?

I have scripted ListDefault.bat to set an environment variable to the logged on user's default printer.

The syntax for using ListDefault.bat is:

\[call\] ListDefault prt

where Prt is a call directed environment variable that will contain the user's default printer.

NOTE: ListDefault.bat uses REG.EXE, built into Windows XP, Windows Server 2003, and later operating systems, or installed from the Windows 2000 Support Tools.

ListDefault.bat contains:

@echo off
if \{%1\}==\{\} @echo ListDefault Printer&goto :EOF
set prt=NONE
for /f "Tokens=1 Delims=," %%a in ('REG query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /V Device^|find "REG_SZ"') do (
 set prt=%%a
set prt=%prt:*REG_SZ=%
set wrk=%prt:~1%
set wrk=%wrk:  =%
if "%wrk:~0,1%" EQU " " set wrk=%wrk:~1%&goto loop
endlocal&set %1=%wrk%

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.