PowerShell script to re-install printer, set to default if it was default

1

We have a printer at work that is giving us problems. We have tried a few different solutions but when a user goes to print to this printer Windows 7 will sometimes display something like:

Windows needs to download and install a software driver from the ... computer

However the PC already has the correct driver. I tried changing a few settings in our group policy according to solutions that came up when I searched this problem but so far nothing has worked. For now I think it would be best to just have Windows reinstall the printer every time the user logs in.

I want the printer to be set to the default printer only if the user already had it set to be the default printer. How can I script this in PowerShell?

0xDECAFBAD

Posted 2012-07-16T15:16:35.930

Reputation: 11

Old question, however if this is a network printer, it seems that the driver is being isntalled at log on, rather than being available to all users This would typically be resolved by installing a "local" printer (but using the cheat method to point the printer port to your networked printer) using an admin account, stating that the printer is available to everyone. the drivers are then installed at the computer level, and the pr3inter available to everyone who logs in? – Kinnectus – 2019-05-28T09:27:53.480

"I want the printer to be set to the default printer only if the user already had it set to be the default printer. " This part is going to be impossible – soandos – 2012-07-17T05:36:49.613

Not impossible, but would require a solution that many people would find unpalatable. You would need to periodically cache which printer is default. The most likely method would be to have a login script cache it to a text file. Then the restore script would just check there to see if it was the default. It means that if the user made the problem printer the default then the printer had the issue before the next login, the script would get it wrong. The OP would have to determine the likelyhood of this edge case. – EBGreen – 2012-07-23T13:31:42.823

Answers

0

This can be achieved using WMI using the Win32_Printer class. Below is an example using powershell, you can do it in vbscript too.

You can adapt the script below to your own needs. The example assumes the driver is called "TOSHIBA e-STUDIO3510cSeriesPCL6"

$driver_name = 'TOSHIBA e-STUDIO3510cSeriesPCL6'

$printers = Get-WmiObject -Class win32_printer -Filter "DriverName='$driver_name'"

foreach ($printer in $printers) { 

    $printer.Delete()

    $return_status = (Invoke-WmiMethod -Class win32_printer -Name AddPrinterConnection -ArgumentList $printer.Name).ReturnStatus

    If ($return_status -eq 0 -and $printer.Default) { 

        (Get-WmiObject -Class win32_printer -Filter "Name='$($printer.Name)'").SetDefault() 
    }
}

What the script does is query which printers are using a particular driver and reinstalls them while maintaining the default printer status if needed.

MFT

Posted 2012-07-16T15:16:35.930

Reputation: 542