I have a script that was created for Windows 2008 R2 which sets the default printer of a host.
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?
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.)
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.
Thank you for your reply.
You must be logged in to reply to this topic.