Script to check if the printer exists locally, and if so, remove it

I am writing a Windows batch file and must check if printing exists on the local computer, and if so, removes the associated printer from the computer. Here is the code that I use to remove the printer.

RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn 

This works fine, but now I need a conditional expression, so I check to see if this printer exists first. Then run this line. I'm not sure how to write this down.

+4
source share
2 answers

You can try something like this, just replace the line to find with the printer you want to find.

 For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I "560C"') Do If "%%I"==",,ServerName,HP DeskJet 560C" goto :REMOVE goto :SKIP :REMOVE RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn :SKIP 

Or just run the command, and if it does not exist, will it be an error if it works?

Hope this helps!

+2
source

You can simply skip the check and run the delete command anyway. To suppress the error pop-up, just add the /q option as indicated in the rundll32 printui.dll documentation . this gives:

 RUNDLL32 printui.dll,PrintUIEntry /n \\server\printerName /dn /q 
0
source

Source: https://habr.com/ru/post/1387392/


All Articles