DrWatson06
DrWatson06

Reputation: 3

Print PDFs to specific printers based on filename

I would just like to preface this by saying I am brand new to Powershell and have been trying to learn by picking things up here and there. I'm currently trying to automate a process within my company using strictly powershell and Adobe reader.

Our company currently is manually printing individual sets of records and a separate cover page, binding them, and sending them off. An idea to automate this process was to fill a folder with a zipped set of .pdfs for the day. This zip file would then be extracted and it's contents moved to another folder. PDFs with the normal set of records listed as "WO-xxxxxx Set" and the cover page as "WO-xxxxxx Cover". All I would need to do is create a simple script that prints these out in order, so that "WO-000001 Cover" is on top of "WO-000001 Set" and then print the next set in the order.

The complication I've run into is that Start-Process -FilePath $File.Fullname -Verb Print only allows me to target a default printer. Our Covers will need to be printed on thicker paper, and as such I thought the best course of action would be to create two printers on the network with the required printer settings. If I could have the script swap between the two printers based on file name then it would solve my issue.

This script is sending the documents to the printer in order but not actually swapping the default printer. I'm sure this is something I've done wrong in my IfElse cmdlet and would appreciate an experts eye in this.

Function UnZipEverything($src, $dest)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
$zps = Get-ChildItem $src -Filter *.zip

foreach ($zp IN $zps)
    {
    $all = $src + $zp
    [System.IO.Compression.ZipFile]::ExtractToDirectory($all, $dest)
    }
}

UnZipEverything -src "C:\Users\admin\Desktop\Zip Test\" -dest'C:\Users\admin\Desktop\UnZip Test\'

Remove-Item "C:\Users\admin\Desktop\Zip Test\*.zip"

$files = Get-ChildItem “C:\Users\admin\Desktop\UnZip Test\*.*” -Recurse

ForEach ($file in $files){
If ($files -eq '*Cover*') {
(New-Object -ComObject WScript.Network).SetDefaultPrinter('Test')
Start-Process -FilePath $File.FullName -Verb Print -PassThru | %{ sleep 10;$_ } | kill
(New-Object -ComObject WScript.Network).SetDefaultPrinter('\\RFC-Print01\Collections Tray 6')
} 

Else {Start-Process -FilePath $File.FullName -Verb Print -PassThru | %{ sleep    10;$_ } | kill
}
}

Any help would be greatly appreciated.

Upvotes: 0

Views: 6353

Answers (1)

Craig Lebakken
Craig Lebakken

Reputation: 1943

If you use the verb PrintTo instead of Print, you can specify the printer:

Start-Process -FilePath $File.FullName -Verb PrintTo '\\RFC-Print01\Collections Tray 6' -PassThru 

This would allow you to remove the SetDefaultPrinter calls from the script.

Upvotes: 1

Related Questions