This is another quickie recipe for you admins. This recipe shows how to get the current user’s default printer via the registry and output the information to a text file via a batch file and in turn combine the result files into a larger file to then import into excel.
This is a pretty basic batch file to get a user’s default printer.
The following code will:
1. create a date variable of the current date in YYYYMMDD format
2. create a user variable based on the users logged in
3. find the default printer via the registry for the user
4. output information into a text file
—START CODE—
@echo off
REM Set drive to save device to, be sure to keep format
REM ie D:\ or E:\
REM Also create current user logged in as user variable
SET saveLoc = “C:\”
for /f “tokens=3 delims=\” %%i in (“% USERPROFILE%”) do (set user=%%i) 2>&1
REM create date variable
FOR /F “TOKENS=1* DELIMS= ” %%A IN (‘DATE/T’) DO SET CDATE=%%B
FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘DATE/T’) DO SET mm=%%B
FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %CDATE%’) DO SET dd=%%B
FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
SET date=%yyyy%%mm%%dd%
FOR /F “TOKENS=2 DELIMS=Z” %%A IN (‘reg query “HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows” /v device’) DO echo %user%:%%A > %saveLoc%%date%_%user%_printer.txt
—END CODE—
so lets say i’m logged on and the batch file is set to run upon startup; a file called: 20070818_Seamonkey420_printer.txt would be created on my C:\ drive.
In this file, you would see something like this (i do not have any real printers installed, only my Adobe PDF printer)
Seamonkey420: Adobe PDF,winspool,Ne02:
Finally, once you have all of the text files w/the default printers in a single directory you can then combine the text files into one big one using this comand:
type *printer.txt > allusers.txt
so lets say i have 3 users and their text files:
20070818_John_printer.txt
20070817_Sally_printer.txt
20070819_Bob_printer.txt
my allusers.txt file will look something like this:
Bob: Adobe PDF,winspool,Ne02:
John: Send to Onenote2003,winspool,Ne02:
Sally: Canon i320,winspool,Ne02:
You can in turn import the allusers.txt file into Excel and use ; as a delimiter. Just open up Excel, go to File > Open. Change the Files of Type to All files and open our allusers.txt file.
When it asks for a delimiter, type in a semicolon (;) and click OK.
Voila, easy spreadsheet of users default printer! Easy printer info gathering!
TIPS/NOTES:
-in the inital batch file, you can set the saveLoc variable to a shared network drive/location to make things easier.
-you could possible implement a runas command and add a function to check all users profiles on a machine and then have the runas command run the reg query command for each user
-this same concept and use of the reg query command can be used to get all kinds of info from a pc such as installed apps, netwokr info, etc.