Need vbs script to get list of printers based on user profile

ryder0707 used Ask the Experts™
Need vbs script to get list of printers based on user profile stored on a PC
I will need to run this script agains remote PCs
Please help
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
can u give me the command u r trying to run in command prompt also the required out so that i can generate the vbs file
Top Expert 2009


I tried to use the code below, but I'm not getting any network printers so I'm thinking might be because the script is querying based on my user id/profile, the user that is using the PC will have many printers available/mapped when he login to the PC
' Title:   List Printers.vbs'  
' Date:    02/23/2010'  
' Author:  Bradley Buskey'  
' Version: 1.00'  ' Updated: 02/23/2010'  
' Purpose: List all printers attached to a workstation'  
Const ForAppending = 8  
Const ForReading = 1   
strComputer = inputbox("Please enter the computer name or IP address.","Computer Name",".")   
Set WshNetwork = CreateObject("WScript.Network")  
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")  
Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")  
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)  
Set WshShell = WScript.CreateObject("WScript.Shell")  
Set objFSO = CreateObject("Scripting.FileSystemObject")   
For Each objItem in colItems      
UserName = objItem.UserName      
arrUserName = Split(UserName, "\", -1, 1)      
varUserName = arrUserName(1)  
filOutput = varUserName & ".txt"   
If objFSO.FileExists(filOutput) Then      
End If   
Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True)  
For Each objPrinter in colInstalledPrinters      
strTest = Left(objPrinter.Name, 2)      
varOpen = MsgBox("Do you want to view the printers?",36,"View File?")  
If varOpen = vbYes Then
varCommand = "notepad " & filOutput      
WshShell.Run varCommand,1,False  
End If   
Wscript.Sleep 1500  
MsgBox "Printer mappings have been stored in '" & filOutput & "'.", 64, "Script Complete"  

Open in new window

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

if printers are being mapped on login, they are likely being mapped by a login script using the Con2Prnt.exe executable, or via GPO, those you will be best to look at the batch script or GPO to determine, as they may not be present except while the user is logged on (as generally you want to delete network printers at log off on a laptop etc)
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Top Expert 2009


Yes that the to grab the info without asking user to login? Anyone?
Must be stored somewhere in registry
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer


  If you are using a logon script to map the printers you will need to look at the logon script.
  If printers are mapped by Group policy, you will need to review the GPO.

That information is not stored on the local computer, it is stored in the script and GPO itself.
Top Expert 2009


Nope you are wrong it is stored locally in the registry, see my other Q
Other expert?
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

This Assumes the printers are not unmapped at log-off, have you confirmed this is the case?  If there is a log-off script which deletes the printers they will not appear in the registry any longer..

Which is  part of why it's easier to check the scripts to see what they are installing and un installing.

However in that thread you suggested doing the enumeration while the user is logged on, which should work, and in this thread you specifically want the information while the user is not logged on.

May I ask what you are trying to accomplish with this script?

I imagine you're trying to troubleshoot an issue where a user either has a printer they do not need or they do not have a printer they need, is this correct?

Lead SaaS Infrastructure Engineer
So if you're confident the printers are not being un-mapped by a process on the computer we can definitely find them in the registry.

I'm not as good at VB Script as I used to be; however here is a Batch file to do what you are looking for.  Certainly a little easier to understand anyway.

Hope that resolves your issues.

:: Script: FindRemotePrinter.Bat                                            				 	  ::
:: Version: 1.0                                                                                   ::
:: Copyright: Ben Personick                                                                       ::
:: Date: 2010-07-06                                                                               ::
::                                                                                                ::
::                                                                                                ::
:: Desc: This script Mounts a Registry Hive on a machine and parses the names of the printers	  ::
::																								  ::

:: This section sets up default variable settings and sets the logging directory and file name.   ::
	CALL :Start-Set-Date
	GOTO :End-Set-Date
		SET TDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
		SET TTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
	SET Preamble=FindRemotePrinter --
	SET SrcUser=USERNAME_To_Find_Printers_Of
	SET SrcShare=C$\Documents And Settings
	SET PLog=%SrcComputer%_%SrcUser%_%TDATE%_%TTIME%.log
	SET IDir=c:\FindRemotePrinter\Log
	SET ILog=ILog_%TDate%_%TTime%.Log
		ECHO %Preamble% Checking For %IDir%
		IF EXIST %IDir% ECHO %Preamble% IDir "%IDir%" -- Found! -- GOTO End-Prep >> %IDir%\%ILog%
		ECHO %Preamble% Creating %IDir%
		mkdir %IDir%



	ECHO --------------------------------------------- >> %IDir%\%PLog%
	ECHO -- Computer: %SrcComputer%					-- >> %IDir%\%PLog%
	ECHO -- User: %SrcUser%							-- >> %IDir%\%PLog%
	ECHO -- Date: %TDate%							-- >> %IDir%\%PLog%
	ECHO -- Time: %TTime%							-- >> %IDir%\%PLog%
	ECHO --------------------------------------------- >> %IDir%\%PLog%
	ECHO. >> %IDir%\%PLog%
	ECHO --------------------------------------------- >> %IDir%\%PLog%
	ECHO -- The Following Printers Were Discovered:	-- >> %IDir%\%PLog%
	ECHO --------------------------------------------- >> %IDir%\%PLog%

		reg LOAD HKEY_USERS\%SrcUser% "\\%SrcComputer%\%SrcShare%\%SrcUser%\NTUSER.DAT"

		ECHO %Preamble% Start-Find-Printer --
		ECHO %Preamble% Start-Find-Printer -- >> %IDir%\%ILog%
		for /F %%A IN ('reg query HKEY_USERS\%SrcUser%\Printers\ /s') DO CALL :Start-Print-Loop %%A
		GOTO End-Find-Printer
			SET PTemp=%1
			IF ERRORLEVEL 1 GOTO Start-IS-Printer
			GOTO END-IS-Printer
				ECHO %PTemp% | FIND /I "Default"
				IF ERRORLEVEL 1 GOTO Start-IS-Printer
				GOTO END-IS-Printer

				ECHO %Preamble% The following Printer was Found - %PTemp% -- And Stored in %IDir%\%PLog% --
				ECHO %PTemp% >> %IDir%\%PLog%
				ECHO %Preamble% The following Printer was Found - %PTemp% -- And Stored in %IDir%\%PLog% -- >> %IDir%\%ILog%
	ECHO %Preamble% The Registry has no more entries at %LookupKey% --
	ECHO %Preamble% The Registry has no more entries at %LookupKey% -- >> %IDir%\%ILog%	


	CALL :Start-Set-Date
	ECHO %Preamble% The Batch File Completed ON %TDATE% AT %TTIME% --
	ECHO %Preamble% The Batch File Completed ON %TDATE% AT %TTIME% -- >> %IDir%\%ILog%	

Open in new window

There's a fundamental problem with getting information from user's profiles.  In order for you to search the registry on a remote PC, the user's profile must be loaded.  This happens normally when a person is logged in... but must be done via code if the PC is powered on without anyone logged in (or in the situations where there are multiple users of a PC and only one is logged in at a time).
Mounting remote registry hives, performing the search, and then unmounting the registry hives is not something that can be easily done via a script.   If you'e interested in a real application, let us know, as this can be done fairly easily in VB.Net/C#, etc.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

@Graye If you will note I have mounted the remote registry hive for an offline user int he script above using standard Microsoft components included with any client XP or greater.  No special coding required.

QCubed... that's a cool solution where you mount the remote registry hive on the local PC.   I always did it the other way... I mounted the hive on the remote PC and then searched the remote registry...
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Thanks!!  =)

It took a little testing to get it to work because I normally work with the online registry not the offline registry.

But I've tested it in my environment and it works just fine as long as you have Administrator permissions to the computer and the user exists.

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer


My batch script functions according to the specifications of the request:

It connects to a remote machine
It loads the registry of a user specified in the script who is not logged on
It finds all the printers of the user which were not unmapped
And additionally outputs them into a log file as well as posting them on the screen.

As I stated above if the printers are uninstalled by some process the settings will be removed from the registry, and therefore there will be no reliable way to finding them.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial