i want a list of my connected printers in CMD

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

i want a list of my connected printers in CMD

#1 Post by bramsharon » 10 Sep 2012 03:20

Hi there!

i made a post earlier similiar to this one, but my boss asked for a bit different case.

He was hoping for an script that can show you automatically all the printers that are connected to that pc so he can select an printer that way as default. i already made a script by hand that shows the printers, but that means i need to adjust the script if another printer is added or if one is deleted, i want this to do this automatically but i really can't find an way. i tried the prnmngr.vbs commands with the -l parameter but it shows me all the information and i can't select one printer in the same command as default printer. you dont need to write the script for me as i want to learn how to do that, but perhaps a suggestion would be helpful.

please help!

Greetings,

Bram

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: i want a list of my connected printers in CMD

#2 Post by foxidrive » 10 Sep 2012 05:08

This works here. Found on the net.

Code: Select all

@echo off
cscript /nologo printerlist.vbs >printerlist.txt


printerlist.vbs

Code: Select all

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")

For Each objPrinter in colInstalledPrinters
    Wscript.Echo "Name: " & objPrinter.Name
    Wscript.Echo "Location: " & objPrinter.Location
    Wscript.Echo "Default: " & objPrinter.Default
Next



If you open powershell and paste this in then you will get a list too.

Code: Select all

get-WmiObject -class Win32_printer | ft name, systemName, shareName >> printers.txt

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: i want a list of my connected printers in CMD

#3 Post by bramsharon » 10 Sep 2012 05:21

thanks for your quick reply.

it doesn't seems to work but im trying! but i was wondering, because i saw the printers.txt.. is this going to create an textfile with the printernames in it? because that's not what i ment. i ment that i want cmd to show all the printers in an menu that is generated automatically and that i can choose out of the available printers the one i want to make default.

thanks for your reply and i really hope you can help me out if it is even possible

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: i want a list of my connected printers in CMD

#4 Post by foxidrive » 10 Sep 2012 06:03

You read the text file and display the printers, when the menu is being displayed.

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: i want a list of my connected printers in CMD

#5 Post by bramsharon » 10 Sep 2012 06:07

yes, but that means that you have to generate an text file for every different pc with different printers?

i create images for different offices. every office has a few printers but i don't want office 1 to interfere with office 2 when it comes to printers.

i want cmd to do an lookup of the connected printers and make a choose menu out of that. i don't want to create/generate files needed for that. it needs to be done automatically.

my question is if this is even possible. it does' nt seem like it or maybe there is an miscommunication. I am dutch so that makes it some how difficult for me to explain things and to understand things.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: i want a list of my connected printers in CMD

#6 Post by foxidrive » 10 Sep 2012 06:13

You don't quite understand how a temporary file is used.

A) you launch your batch file.
B) your batch file interrogates the current PC for a list of printers and places it in a temporary file.
C) your batch file then reads the file and displays the menu using the printer names.
D) your user selects the printer number (1/2/3/4/5/6).
E) your batch file deletes the temporary file and continues on with what you need to do.

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: i want a list of my connected printers in CMD

#7 Post by bramsharon » 10 Sep 2012 06:17

oh alright! then it is what i am looking for!

that's exactly what i meant.

i hope i can figure this out. i am new to scripting so its gonna be tough i think but at least i know its possible.

thanks a lot!

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: i want a list of my connected printers in CMD

#8 Post by bramsharon » 10 Sep 2012 06:28

i can't figure it out how it works. i cant find the printerlist.vbs and that command under it doesn't seem to work just like the first one which makes the output.

i really want to learn how it works

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: i want a list of my connected printers in CMD

#9 Post by foxidrive » 10 Sep 2012 07:16

Try modifying this. The script is echoed to a file and then run. The output is typed to the screen and then the input is taken.

In the script the & are replaced with ^& to escape them, so they aren't considered as batch command separators.

EDITED: to return the actual printer name
EDITED AGAIN: to handle over 9 printers (changed find to findstr)
EDITED ONCE MORE: to add a command to set the default printer: untested (RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "printer name")

Code: Select all

@echo off
set "vbsscript=printerlist.vbs"

>  "%vbsscript%" echo strComputer = "."
>> "%vbsscript%" echo Set objWMIService = GetObject("winmgmts:" ^& "{impersonationLevel=impersonate}!\\" ^& strComputer ^& "\root\cimv2")
>> "%vbsscript%" echo Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
>> "%vbsscript%" echo num=1
>> "%vbsscript%" echo For Each objPrinter in colInstalledPrinters
>> "%vbsscript%" echo     Wscript.Echo num ^& ") " ^& objPrinter.Name
>> "%vbsscript%" echo     num = num + 1
>> "%vbsscript%" echo Next

cscript /nologo "%vbsscript%" >printerlist.txt

type printerlist.txt

del "%vbsscript%"
echo.
:loop
set "in="
set /p "in=Type the printer number (or just ENTER to quit): "
if not defined IN (
del printerlist.txt
goto :EOF
)
for /f "tokens=1,*" %%a in ('findstr /r "^%in%)" ^< "printerlist.txt"') do set "printer=%%b"
del printerlist.txt
echo.
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%printer%"
echo Default Printer is now "%printer%"
pause

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: i want a list of my connected printers in CMD

#10 Post by bramsharon » 10 Sep 2012 13:34

thanks a lot for your help! as its evening here and i can't really work on it till tomorrow, what do i have to modify? im sorry for being an idiot but its all new to me but i want to understand. you don't need to explain if you don't want, im just curious what i should edit.

thanks a lot for your help!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: i want a list of my connected printers in CMD

#11 Post by foxidrive » 10 Sep 2012 17:13

Oh, I thought you had code to make a printer the default.

Try googling for that code so you can add it to the simple menu in my last post.

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: i want a list of my connected printers in CMD

#12 Post by bramsharon » 11 Sep 2012 01:20

thanks for your help, but i don't see what i am doing wrong. it just doesn't work.

i've build a huge script and this part is the last missing piece of the puzzle. so i am trying this on a different empty .bat file. but it just doesn't work.

thank you guys for your patience and help. and yes i am an 'noob' at this. but i accepted this task because i want to learn about scripting.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: i want a list of my connected printers in CMD

#13 Post by foxidrive » 11 Sep 2012 01:59

Try the code I provided above - I edited it to give you the printer name, and not just the number that was entered. Sorry for confusing that.

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: i want a list of my connected printers in CMD

#14 Post by bramsharon » 11 Sep 2012 05:01

hey foxidrive!

it worked! it now reads my printers! only one thing,

i have an script that changes the pc name. i let the user fill in the name himself and i want that name/number of the pc to be send automatically to where you have to fill in the name in this script. it's gonna be one big script. is that possible? and how?
thanks alot!

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: i want a list of my connected printers in CMD

#15 Post by bramsharon » 11 Sep 2012 05:05

oh i more thing, it does list my printers but doesn't make the chosen one the default printer. it just accepts my input and thats it.

Post Reply