Microsoft Printer Management Using PowerShell - Windows Printing Team Blog - Site Home - TechNet Blogs

Windows Printing Team Blog

Setup, connect, and print with confidence

Printer Management Using PowerShell

Printer Management Using PowerShell

  • Comments 17
  • Likes

*******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
  • 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

    kai

  • 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]

  • Justin ,

    Could you please post un exemple with a new schema. I have do modify the PagePerSheet property an it doesn't work.

    Thanks

    Mathieu

  • 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,$permAdminPrinter

  • Nice post, I tweaked the code a bit so I could pull queues from my print server ps but am getting an error.

    $host.Runspace.ThreadOptions = "ReuseThread"

    Add-Type -AssemblyName System.Printing

    $server = new-object System.Printing.PrintServer("\\ps")

    $queues = $server.GetPrintQueues(@([System.Printing.EnumeratedPrintQueueTypes]::Shared))

    Error:

    Add-Type : Length cannot be less than zero.

    Parameter name: length

    At line:1 char:9

    + Add-Type <<<<  -AssemblyName System.Printing

       + CategoryInfo          : NotSpecified: (:) [Add-Type], ArgumentOutOfRangeException

       + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.AddTypeCommand

    I'm running this on windows 7 x64 enterprise. I have all the rsat tools installed, but I figure I must be missing something.

  • I wrote up some stuff, hope you enjoy

    gallery.technet.microsoft.com/.../PrintServerManagementps1-7676ed28

  • Jeffrey, thanks a lot.  That is helpful content!

  • <p><a href="http:// www.flyerprintersuk.com"  > flyer printers uk </a></p>is a business leader in handling  any type of print projects for marketing needs.  

    Our products are brochures, catalogs, postcards,

    Business cards, banners, plastic cards, leaflet , sticker etc.

    Affordability, high quality and fast turnarounds are our area of focus.

  • Can someone please provide assistance in adapting this script to check the Printer Driver version?

  • Be sure to check out the new PowerShell scripts provided in Windows 8 Consumer preview for printer management. technet.microsoft.com/.../hh918357.aspx

  • Justin -

    I get an error at the following line of your script:

    $q2 = New-Object System.Printing.PrintQueue $server,$q.Name,$queuePerms

    The error reads something like this:

    "Attempt by security transparent method 'MS.Internal.PrintWin32Thunk.PrinterDefaults.Dispose(Boolean)' to access security critical method 'MS.Internal.PrintWin32Thunk.PrinterDefaults.InternalDispose(Boolean)' failed."

    I've googled this and found that it's a known bug in the .NET v4.0 assembly System.Printing, and the workarounds I've found do not work for me... Any pointers?

    Thanks,

    Brendan

  • 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...

    -Shweta

  • we offered cheap and best high quality printing services like business card, gifts, T-shirts, mugs , stationary products, corporate products, etc. only at premiquickprints.com

  • Hi

    This scrip work only with local printers. How can I change network printer installes on my computer with PowerShell?

  • 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,

    Ninad

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment