I got a question from Mark this week:
“Hi Steve,
Love your newsletter, keep sending me the good stuff 🙂 I have a question for you. I was wondering if there was a way to delete printers that were offline from a script. This would allow me to clean up dead printers without going around to each computer. My google fu has not been able to turn anything up. – Thanks!”
Excellent question Mark. It took me quite a while to find it, but there is a property in the Win32_Printer object. It is called ExtendedPrinterStatus.
I was able to figure out through experimentation that if ExtendedPrinterStatus is equal to 7, then the printer is offline.
With that info in hand, we can build a simple vb-script that will delete the printer if it is offline:
set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
set sInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer where ExtendedPrinterStatus = 7")
For Each sPrinter in sInstalledPrinters
sPrinter.Delete_
Next
Notice the underscore in the delete method – that is needed. Just a little twist added by Microsoft to keep you on your toes. They roll that way
To call it from your script, use the cscript.exe executable. A good example would look like this:
%windir%\system32\cscript.exe \\server\path\offline_printer_cleanup.vbs
Finally – Don’t leave this thing in place all the time. Once you think enough users have logged in and all is cleaned up, I would suggest removing it from their logon script.
Why?
What happens when one of your main printers goes offline?
Yep, those calls start rolling in about a missing printer.
If you are having trouble using cut and paste from the browser, here is a link to the script on our website:
Make sure to rename to .vbs after downloading.
One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools
{ 5 comments… read them below or add one }
Thanks for the tip Steve. Don’t know how you found that ExtendedPrinterStatus but I sure will put it to use!
Will this work on Windows XP?
Hi Markus,
It will work fine on XP. Not sure about 2000. I don’y have a 2000 machine here up and running to test anymore 🙂
Will it work on windows 7 64 bit
Yes it will. It should work on all versions of windows up to Windows 10