Printer Management Using PowerShell
*******Disclaimer. This posting contains scripting samples. These are
provided as-is with no guaranties or warranties of any kind. They are
not thoroughly tested in all scenarios.
Over the years, the Printing team has released a number of management technologies designed to help print administrators get their jobs done. While the Print Management Console (PMC) is the tool of choice for many, sometimes you need a scripted solution. This is where PowerShell comes in.
One of the beautiful things about PowerShell is that it provides a single entry point to a wide array of scripting tools. For instance, you can manage WMI objects, .NET objects, call console apps like SetPrinter.exe and even (if you’re adventurous) marshal native API calls through .NET to PowerShell. For the purposes of this post, though, I’m going to keep things planted firmly in what you can do using the objects provided in .NET 3.0+.
One common administrative scenario would be setting all of the print queues on a print server to print double sided or duplexed. We could do this in the PMC, but we would need to edit each queue one-by-one: not fun. Fortunately, the Print Schema (introduced in Windows Vista) provides a common syntax that we can script against, and .NET provides a number of convenient objects to represent some of the common keywords.
Let’s go to the code. First, we need to change our threading model so that everything plays nice. Then we get into the real work of importing the correct namespace (System.Printing) and set up some objects we’ll be using. Note that we're filtering the queues we get back from the (local) server using a flag from System.Printing.EnumeratedPrintQueueTypes so that we only get shared queues. You could add more flags into the array we pass to GetPrintQueues() as well.
$host.Runspace.ThreadOptions = "ReuseThread"
Add-Type -AssemblyName System.Printing
$permissions = [System.Printing.PrintSystemDesiredAccess]::AdministrateServer
$queueperms = [System.Printing.PrintSystemDesiredAccess]::AdministratePrinter
$server = new-object System.Printing.PrintServer -argumentList $permissions
$queues = $server.GetPrintQueues(@([System.Printing.EnumeratedPrintQueueTypes]::Shared))
Now that we have a collection of PrintQueue objects in $queues, we can iterate over them in a foreach loop. The first challenge we’ll need to deal with is getting a PrintQueue object where we have administrative privileges. Note: You will most likely have to run PowerShell as an administrator in order to get permission to modify the PrintQueue object.
foreach ($q in $queues) {
#Get edit Permissions on the Queue
$q2 = new-object System.Printing.PrintQueue -argumentList $server,$q.Name,1,$queueperms
}
From here it is just a matter of finding devices that support the desired PrintCapability and then setting it to the desired value. We'll be using the DuplexingCapability object in this example.
$duplexCaps = $q2.GetPrintCapabilities().DuplexingCapability
if ($duplexCaps.Contains([System.Printing.Duplexing]::TwoSidedLongEdge)) {
$q2.DefaultPrintTicket.Duplexing = [System.Printing.Duplexing]::TwoSidedLongEdge
$q2.commit()
}
Putting it all together:
$host.Runspace.ThreadOptions = "ReuseThread"
Add-Type -AssemblyName System.Printing
$permissions = [System.Printing.PrintSystemDesiredAccess]::AdministrateServer
$queueperms = [System.Printing.PrintSystemDesiredAccess]::AdministratePrinter
$server = new-object System.Printing.PrintServer -argumentList $permissions
$queues = $server.GetPrintQueues(@([System.Printing.EnumeratedPrintQueueTypes]::Shared))
foreach ($q in $queues) {
#Get edit Permissions on the Queue
$q2 = new-object System.Printing.PrintQueue -argumentList $server,$q.Name,1,$queueperms
#Set DefaultPrintTicket
$duplexCaps = $q2.GetPrintCapabilities().DuplexingCapability
if ($duplexCaps.Contains([System.Printing.Duplexing]::TwoSidedLongEdge)) {
$q2.DefaultPrintTicket.Duplexing = [System.Printing.Duplexing]::TwoSidedLongEdge
$q2.commit()
}
}
This is of course, just one example of what you could do now that you've got the System.Printing namespace mapped into PowerShell.
Thanks,
Justin Hutchings & Alan Morris
Windows-Printing
Comments
Anonymous
January 01, 2003
The comment has been removedAnonymous
January 01, 2003
Kai, The driver you are working with may not support the Duplexing feature in the public namespace of the PrintSchema. You can confirm whether or not the feature is available on the PrintQueue by adding an else statement after the if statement. If that's the case, you'll have to alter the DefaultPrintTicket's XML directly to manage it. The PrintSchema Specification has would be a good reference if you need to do that. Thanks Justin [MSFT]Anonymous
January 01, 2003
Be sure to check out the new PowerShell scripts provided in Windows 8 Consumer preview for printer management. technet.microsoft.com/.../hh918357.aspxAnonymous
January 01, 2003
Jeffrey, thanks a lot. That is helpful content!Anonymous
January 01, 2003
I wrote up some stuff, hope you enjoy gallery.technet.microsoft.com/.../PrintServerManagementps1-7676ed28Anonymous
January 01, 2003
Dear Justin Hutchings & Alan Morris, I tried your Powershell source Code to change from Simplex to duplex. I do not get any errors, but the Driver is still simplex. I tried to change these settings with WMI (win32_printerconfiguration) but I was unsuccessfull. Please do not misunderstand, I will not have a script which works... :-) I am just looking for a possibilitie to change from simplex to dupelx and from color to B/W. Do you think there is a possibilitie? All classes which I found are readOnly... I like this Blog so please update it. thanks and regards kaiAnonymous
January 01, 2003
Can someone please provide assistance in adapting this script to check the Printer Driver version?Anonymous
January 01, 2003
Hi, I need a script to change the printer settings mode to duplex or both sides. This will help me in saving paper, cause accidendly a couple of times I have sent the print job without changing the settings. Thanks in advance... -ShwetaAnonymous
February 24, 2011
Justin , Could you please post un exemple with a new schema. I have do modify the PagePerSheet property an it doesn't work. Thanks MathieuAnonymous
May 11, 2011
The scripts in this post are a good start, but I couldn't actually make things work till I did the following: $host.Runspace.ThreadOptions = "ReuseThread" Add-Type -AssemblyName System.Printing $permAdminServer = ([System.Printing.PrintSystemDesiredAccess] "AdministrateServer") $permAdminPrinter = ([System.Printing.PrintSystemDesiredAccess] "AdministratePrinter") $ps = New-Object System.Printing.LocalPrintServer $permAdminServer $queue = $ps.GetPrintQueues() | Select-Object -Last 1 $queue2 = New-Object System.Printing.PrintQueue $ps,$queue.Name,$permAdminPrinterAnonymous
February 09, 2012
The comment has been removedAnonymous
June 07, 2012
The comment has been removedAnonymous
May 10, 2013
The comment has been removedAnonymous
July 21, 2013
Hi This scrip work only with local printers. How can I change network printer installes on my computer with PowerShell?Anonymous
October 14, 2013
I want to get security permission Administrators group on printer object in windows 2003 using power shell. i am not able to do by power shell as get security Descriptor method is not present in windows 2003 can any one help me to get this done using power shell Any inputs are much appreciated Thanks and Regards, NinadAnonymous
December 12, 2013
Hi, I know this is an old thread but I'll give it a try - I have the same question as Michal - how can I do this on network printers on my computer?Anonymous
February 06, 2014
The Samsung ML-2955DW mono laser printer is notable for its very low street price and its full network connectivity, namely USB, ether-net and Wi-Fi.Cloud computing in gold coastAnonymous
September 10, 2014
Hello Justin
Can u tell us how enable the same duplexing in a remote computer through pssession in powershell.Anonymous
September 22, 2015
This really answered my problem, thank you!