Using a PowerShell Script to change the default tray selected on a printer

Viewed 133

I've been looking online at ways to automatically change the default tray on a printer. I'm in need of this because my job uses printers via an old IBM server, and these specific printers don't seem to respond to any instruction from the IBM server to change the tray used for the job. My only option to automatically change the preferences of a printer seems to be through a PowerShell script. I found a script on Stack that seems to achieve most of the functionality I want, however, I have very limited scripting knowledge and as a result, I'm unsure how to add the additional functionality required. First off, here's the code:

$Printer = "Example Printer Name"
$InputBin = "AutoSelect","AutoSheetFeeder","Cassette","Manual","Tractor" #choose one
Add-Type -AssemblyName System.Printing
$Permissions = [System.Printing.PrintSystemDesiredAccess]::AdministrateServer
$QueuePerms = [System.Printing.PrintSystemDesiredAccess]::AdministratePrinter
$PrintServer = new-object System.Printing.LocalPrintServer -ArgumentList $Permissions
$NewQueue = New-Object System.Printing.PrintQueue -ArgumentList $PrintServer,$Printer,1,$QueuePerms
$InputBinCaps = $NewQueue.GetPrintCapabilities().InputBinCapability
if ($null -ne $InputBinCaps) {
    if ($InputBinCaps.Contains([System.Printing.InputBin]::$InputBin)) {
        $NewQueue.DefaultPrintTicket.InputBin = [System.Printing.InputBin]::$InputBin
        $NewQueue.UserPrintTicket.InputBin = [System.Printing.InputBin]::$InputBin
    } else {
        Write-Error "$InputBin unavailable on $Printer"
    }
}
$NewQueue.commit()
$NewQueue.dispose()
$PrintServer.commit()
$PrintServer.dispose()

What I need to add is a way to detect what tray is currently set to default and use that data to direct an if statement to change it to the opposite (I only have two trays on these printers, Cassette 1 and Bypass Tray). Additionally, I need the script to immediately close its window upon execution of the task. The reason being is that I plan to put this as a shortcut on their Windows taskbar and have them click the shortcut every time they need to switch printer trays. I understand this setup is jank, but it is the only way to get the correct type of paper printed, save opening up the default tray, and laying one yellow sheet on top.

Any help would be greatly appreciated.

Thanks

0 Answers
Related