Author Posts

March 20, 2015 at 8:17 am

I have a script that was created for Windows 2008 R2 which sets the default printer of a host.
This script is used for large scale environments and uses the Get-WMIObject method to retrieve a list of printers.

Due to some issues with a print driver, we need to utilize the new get-printer method but I CANNOT locate how to set a default printer with data pulled using get printer.

Here is an example

OLD CODE: (WORKS)

$printers = get-wmiobject win32_printer
$defaultprinter = $printers | get-random
$defaultprinter.setDefaultPrinter()  

NEW CODE (DOES NOT WORK)

$printers = get-printer
$defaultprinter = $printers | get-random
$defaultprinter.setDefaultPrinter() 

The new code fails due to the way get-printer returns results. I have tried limiting to $defaultprinter.name and other variations with no luck

Is anyone aware of a different method of setting the default printer with output from get-printer?
I have exampled set-printer, set-printerproperty with no luck

March 20, 2015 at 8:26 am

Default printers are a per-user setting, so this probably isn't going to work quite the way you expect it to.

The new CIM classes that Get-Printer returns don't have a method to set the default printer. You can either fall back to WMI, or use the old WScript.Network COM object. For example:

$wshNet = New-Object -ComObject WScript.Network
$wshNet.SetDefaultPrinter($defaultprinter.Name)

Either way, though, you're only setting the default printer for yourself (or whatever user is running this script code.)

March 20, 2015 at 8:32 am

Thank you for the reply. Yes understood its only for my self. Basically what I am doing is running this on thousands of workstations to grab a random printer from printers that are already mapped and make a random one the default printer.
This is for a very specific purpose. Unfortunately there is no method using the new classes. The WMI way causes an undesirable result on our print server at this large of a scale. I have proven that the get-printer method has much less of an impact.

Thank you for your reply.