OK, So I want to pull the existing printers from the computer before imaging, but since its running the KBE, I can't use the MS VBS files. So I tried to pull them from the registry using the following commands (I will change them to match the registry of the offline Windows installation once in the KBE):

reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers | findstr /i "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)">a.txt
FOR /f "usebackq delims=}" %p IN (`reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers ^| findstr /i "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)"`) DO reg query "%p\DsSpooler" /v portName | findstr "portName">>a.txt 

I can get the following output file, a.txt:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\HP Color LaserJet CP5220 Series PCL6
    portName    REG_MULTI_SZ

Now, what I need to do, is look in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\ and pull the HostName or IPAddress keys (just in case some creative tech made the name different from the actual address), but my problem is (I'm embarased to admit) manipulating the output to get JUST a) the name of the printer ("HP Color LaserJet CP5220 Series PCL6") and b) its portName ( into a usable format so I can plug it back into reg.exe and get the actual address. Any help? I know this would be easier with VBS but its been forever and a day since I used VB; I considered PowerShell but I dont know if its built into the KBE. If you can forgive my archaic methods I would be forever grateful.

0 Comments   [ + ] Show Comments


Please log in to comment


I tested this on my PC, but I doubt it will work on a wide scale basis. Also, I don't have anything under the standard tcp/ip ports even though I have a wireless printer installed.


REM Skip the DefaultSpoolDirectory and LANGIDOfLastDefaultDevmode lines
FOR /F "SKIP=5 TOKENS=*" %A IN ('REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers') DO (CALL :PRINTERINFO %A)

REG QUERY "%*" | FINDSTR /I "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)" > nul

FOR /F "SKIP=1 TOKENS=7 DELIMS=\" %A IN ('REG QUERY "%*\DsSpooler" /v portName') DO (ECHO %A>> a.TXT)
FOR /F "SKIP=2 TOKENS=3" %A IN ('REG QUERY "%*\DsSpooler" /v portName') DO (ECHO %A>> a.TXT)



Answered 09/05/2015 by: flip1001
Third Degree Green Belt

Please log in to comment
It would be *much* easier in VBS, you're right.

To simplify the string manipulation, I'd first use Split with 'portName' as the separator, then use Split again on the first element of the returned array but this time using '\' as the separator. However, it would be a lot easier to use the registry class which I have posted a quadzillion times here on ITNinja and just get the values you want from the registry, using a For Each loop to...errrr...loop through the 'Printers' key. Actually, there's a function in that class that will retrieve the value names and the associated data and put it into an array.
Answered 09/06/2015 by: VBScab
Red Belt

Please log in to comment
Answer this question or Comment on this question for clarity
Admin Script Editor
Admin Script Editor is an integrated scripting environment available free here at ITNinja