Posted on 10-25-2016 12:44 AM
Is there a way that we can delete printer that user added? we have a open directory that teacher can pick what printer they want to add. now we have new system that only uses one virtual printer, and we already push that one using LPD. what we want to happen is to remove all printer that they add and only printer one printer will stay the one that we push.
Solved! Go to Solution.
Posted on 10-25-2016 01:14 AM
Removing all printers from a Mac before adding new ones can be done with this script.
#!/bin/bash
lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done
Posted on 10-25-2016 01:14 AM
Removing all printers from a Mac before adding new ones can be done with this script.
#!/bin/bash
lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done
Posted on 02-05-2023 01:22 AM
This script has broken the printer setting pane.
Now I can't see any new printer after adding it.
I tried to right-click and reset printer settings or to reset the system preferences completely and nothing helped.
Please help me
Posted on 10-25-2016 01:24 AM
@franton how about deleting specific printer like not all the printer only the one that they added using our open directory?
Posted on 10-25-2016 01:26 AM
@jamfmdm Well as long as you know the name of it, that's a simple "lpadmin -x printername" on it's own. That assumes the printer isn't in Casper already, otherwise you could use a policy scoped to that machine to remove it.
Posted on 10-25-2016 01:57 AM
Tried this and it work it deleted all the printer
lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done
When i tried this it did not delete the printer that i want
lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" UASD-PRINTER-SF-3111-Xerox-4112
lpadmin -x UASD-PRINTER-SF-3111-Xerox-4112
done
Posted on 10-25-2016 03:05 AM
@jamfmdm Ok what happens if you just run this on a terminal window?
sudo lpadmin -x UASD-PRINTER-SF-3111-Xerox-4112
Posted on 10-25-2016 03:17 AM
@franton This is what happen.
Posted on 10-25-2016 03:18 AM
Ok interesting. What's the output from
sudo cat /etc/cups/printers.conf
Posted on 10-25-2016 03:56 AM
@franton i got it. it should be GF_Printer_SF_3111_Xerox_4112 for the printer name
Posted on 10-25-2016 03:59 AM
Excellent. I had a feeling it was something like that.
Posted on 10-25-2016 11:07 PM
@franton hello i have one more question. i made the script something like this
lpadmin -x UASD_PRINTER_GF_Copy_Center_Xerox4112
lpadmin -x UASD_PRINTER_SF_3111_Xerox_4112
lpadmin -x UASD_PRINTER_FF_2108_XeroxPro275
lpadmin -x UASD_PRINTER_SF_312A_HPLJ4014
lpadmin -x UASD_PRINTER_MPH_M12_Xerox5845
lpadmin -x UASD_PRINTER_GF_Copy_Center_SharpColor_MX_5110n
this is the printer we want to remove to all the teacher, but some printers are not installed to teacher laptop and some are installed, but for some reason when i push the policy most of the macbook is failed and this is the error message that is showing.
Posted on 10-25-2016 11:18 PM
Yes that will happen if you attempt to remove a printer that doesn't exist on the system. You will have to script around this.
Posted on 10-25-2016 11:23 PM
@franton so there is wrong with my script right that why it's not working? but for some are working not sure why.
Posted on 10-25-2016 11:25 PM
@franton so there is wrong with my script right that why it's not working? but for some are working not sure why.
Posted on 10-25-2016 11:26 PM
It depends entirely on how those printers were originally created on those macs. You've already found that the display name of the printer doesn't always match the CUPS name with the _ instead of - . Hardcoding the printer names will produce uneven results because if they were manually created, then human error during creation will produce different results.
That's why I recommended removing all printers before adding your replacement printer queue.
Posted on 10-25-2016 11:28 PM
If you go back to a Mac where your script failed on, and run:
lpstat -p | awk '{print $2}'
That will give you a list of all the CUPS printer names on the Mac. You can then compare to your script. I'm almost certain you will find differences in the names.
Posted on 08-31-2022 08:47 AM
Hi, the script is giving me this error:
Script result: Deleting Printer: Xerox_C60
Deleting Printer: Xerox_C60 lpadmin: The printer or class does not exist.
Deleting Printer: Xerox_C60 lpadmin: The printer or class does not exist.
Deleting Printer: Xerox_C60 lpadmin: The printer or class does not exist.
Deleting Printer: Xerox_C60 lpadmin: The printer or class does not exist.
Anyway the name is correct... any tips?
Posted on 04-26-2017 08:47 AM
hello, i don´t know if this wasn't before, but
lpstat -p | awk '{print $2}'
will output the Queue Name in quotes and that results in not finding that printer when using
#!/bin/bash
lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done
with Sierra.
Anyone having the same problem?
Anyone with enough awk skills to remove the quotes?
Posted on 05-30-2018 05:51 AM
@jensm We had some „ and “ and used successfully:
lpstat -p | cut -d' ' -f2 | tr -d „ | tr -d “ | xargs -I{} lpadmin -x {}
Posted on 05-30-2018 06:29 AM
You can also use wildcards to remove the printers:
printers=($(lpstat -p | awk '{print $2}' | sed '/^$/d'))
for i in "${printers[@]}"
do
if [[ ${i} == *"2831 C9"* ]]; then
lpadmin -x ${i}
elif [[ ${i} == *"2831_C9"* ]]; then
lpadmin -x ${i}
fi
done
Posted on 01-09-2020 09:20 AM
Franton... Thank you. Your input on this subject helped me tremendously!
Posted on 01-28-2021 07:04 AM
@stevewood, I like what you posted but I'm guessing (because I'm not great at scripting) that your snippet is only looking for 1 instance of the printer name containing "2831 c9". How could I adapt this to loop through and delete all printers with "2831 c9" in their name?
Posted on 01-28-2021 09:01 AM
Actually, the wildcards before and after each of those should match any printer with that in the name. So, this line:
if [[ ${i} == "2831 C9" ]]; then
Is saying "if the printer name contains 2831 C9, then you may proceed". And this line:
elif [[ ${i} == "2831_C9" ]]; then
is saying "otherwise (else) if the printer name contains 2831_C9, then you may proceed".
The asterisk before and after those names means match anything that contains the value in quotes. If you had an asterisk only at the front it would match names that end with the pattern, and if the asterisk was only at the end it would match names that start with the pattern.
Make sense?
Posted on 01-29-2021 05:24 AM
@stevewood Makes sense. Thanks for the explaination.
Posted on 08-25-2021 02:34 PM
Follow up to my original answer. If I just want to nuke all printers, i'm now running this:
/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework/Versions/A/printtool -f --reset
Posted on 08-25-2021 02:50 PM
@franton - is this the same as Control-Clicking within the Printers section of "Printers & Scanners" and selecting "Reset printing system..."?
Posted on 08-25-2021 02:56 PM
Precisely that!
08-25-2021 02:57 PM - edited 08-25-2021 02:58 PM
I can't tell you how long I've looked for a way to do that! Cheers, sir!