Share via


SetPrint.ps1: A Powershell Script to Set Some Default Settings on Print Queues

Here's a script that sets the following default settings for all the print queues on your Windows Server 2008 R2 and Windows Server 2008 print server.

Currently, you can set the following default settings: color, mono, twosided, onesided, and staple.

What would you do to improve this script? Any ideas? Post your updates here!

Thanks

param ($ChangeProp)

$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()

function SetProp($capability, $property, $enumeration)
    {
        if ($PrintCaps.$capability.Contains($property))
                            {       
                               $q2.DefaultPrintTicket.$enumeration = $property  
                                $q2.commit()
                                write-host ($q.Name +" is now configured for " + $property) 
                          }
                        else
                            {
                                write-host ($q.Name +" does not support " + $property)
                  
                            }
    }
   
       
try {
 foreach ($q in $queues)
        {
            #Get edit Permissions on the Queue
            $q2 = new-object System.Printing.PrintQueue -argumentList $server,$q.Name,1,$queueperms
           
            # Get Capabilities Object for the Print Queue
            $PrintCaps = $q2.GetPrintCapabilities()
            
            switch ($ChangeProp)
             {
                {$_ -eq "twosided"}
                    {
   
                       SetProp DuplexingCapability TwoSidedLongEdge Duplexing
                       break
                    }
                   
                    {$_ -eq "onesided"}
                    {
   
                       SetProp DuplexingCapability onesided Duplexing
                       break
                    }
                   
                    {$_ -eq "mono"}
                    {
                           
                        SetProp OutputColorCapability monochrome OutputColor
                        break
                      }
                   
                {$_ -eq "color"}
                    {
                           
                        SetProp OutputColorCapability color OutputColor
                        break
                      }
                {$_ -eq "staple"}
                    {
                           
                        SetProp StaplingCapability StapleTopLeft Stapling
                        break
                      }
                default
                    {
                        Write-Host ("$_ is not a valid parameter")
                        exit
                    }
               
                }
   
        }
    }
catch [System.Management.Automation.RuntimeException] {
write-host ("Exception: " + $_.Exception.GetType().FullName)
write-host $_.Exception.Message
}

Here is an update which adds a "show" parameter to show the properties of the print queues:

param ($ChangeProp)

$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()

function SetProp($capability, $property, $enumeration)
    {
        if ($PrintCaps.$capability.Contains($property))
                            {       
                           $q2.DefaultPrintTicket.$enumeration = $property  
                        $q2.commit()
                               
                                write-host ($q.Name +" is now configured for " + $property)
                                write-host (" ") 
                          }
                        else
                            {
                               
                                write-host ($q.Name +" does not support " + $property)
                                write-host (" ")
                  
                            }
    }
   
       
try {
 foreach ($q in $queues)
        {
            #Get edit Permissions on the Queue
            $q2 = new-object System.Printing.PrintQueue -argumentList $server,$q.Name,1,$queueperms
           
            # Get Capabilities Object for the Print Queue
            $PrintCaps = $q2.GetPrintCapabilities()
           
           
           
            
            switch ($ChangeProp)
             {
                {$_ -eq "twosided"}
                    {
   
                       SetProp DuplexingCapability TwoSidedLongEdge Duplexing
                       break
                    }
                   
                    {$_ -eq "onesided"}
                    {
   
                       SetProp DuplexingCapability onesided Duplexing
                       break
                    }
                   
                    {$_ -eq "mono"}
                    {
                           
                        SetProp OutputColorCapability monochrome OutputColor
                        break
                      }
                   
                {$_ -eq "color"}
                    {
                           
                        SetProp OutputColorCapability color OutputColor
                        break
                      }
                {$_ -eq "staple"}
                    {
                           
                        SetProp StaplingCapability StapleTopLeft Stapling
                        break
                      }
                {$_ -eq "show"}
                    {
                           
                        $DefaultTicket = $q2.DefaultPrintTicket
                        $TicketProps = $DefaultTicket | Get-Member -MemberType Property
                                              
                        write-host (" ")
                        write-host ($q.name)
                       
                        foreach ($p in $TicketProps)
                        {
                         $PName = $p.name
                         $PropValue = $DefaultTicket.$PName
                         write-host ($p.name + " = " + $PropValue)
                        }
                      }
               
                default
                    {
                        Write-Host ("$_ is not a valid parameter")
                        exit
                    }
               
                }
   
        }
    }
catch [System.Management.Automation.RuntimeException]
 {
    write-host ("Exception: " + $_.Exception.GetType().FullName)
    write-host $_.Exception.Message
 }