Hi all,

I’m after a PowerShell script that will do the following in order

  1. Search for all documents in C:\Users%CurrentUsers%\Downloads*.pdf

  2. Print these pdf files to the default printer

  3. Delete the files after the print job has been complete

Thank you for your assistance :slight_smile:

6 Spice ups

No problem, I’m sure we can assist you, but we wont write it for you, so please share with us, what you already have.

What works and what does not.

1 Spice up

I’m quite new to PowerShell so i do apologise . My current script that I’ve tried is the following

Get-ChildItem “C:\Users\adadmin\Downloads*.pdf” |

ForEach-Object {Start-Process -Verb Print}

This then asks for the file path again which i stated as "C:\Users\adadmin\downloads*.pdf " which I would then assume should print everything with the file extension .pdf but it doesn’t and comes back with the following

" Start-Process : This command cannot be run due to the error: No application is associated with the specified file for
this operation.
At line:1 char:67

  • … \adadmin\Downloads*.pdf" |ForEach-Object {Start-Process -Verb Print}
  • CategoryInfo : InvalidOperation: (:slight_smile: [Start-Process], InvalidOperationException
  • FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand"

I nailed this down to it has no default program to access the file so I installed adobe reader, set this as the default application for .pdf and this resolved the issue. My first problem is I’m having to manually input the file path again and unsure how to get around this.

Secondly i am wanting this to wait until the document is printed and then delete the file

I’m currently using the following

C:\users\adadmin\downloads*.pdf -force which deletes but does no checking or waiting.

Current script layout is

"

Get-ChildItem “C:\Users\adadmin\Downloads*.pdf” |

ForEach-Object {Start-Process -Verb Print}

—manualy inputting file path again-- I want to automate this somehow

Timeout 15 second ( this allows me the file time to process/print but im wanting a way to check if its printed before moving to the deletion process )

Remove-Item -Path C:\users\adchandler\downloads*.pdf -force

Your errir is:

At line:1 char:67
+ ... \adadmin\Downloads\*.pdf" |ForEach-Object {Start-Process -Verb Print}
+                                               ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

This is telling you that the code "start-process -verb print) i invalid.The reason is that you also need to pass a file name so something like this might work:

ForEach-Object {Print.exe $_.fullname}

As a tip, when posting code, please use the insert code </> option above your text box and specify the type of code, so it formats correctly, as per tfl’s reply.

Thank you very much for you input TFL. I added your recommendation and it now prints each time without fail!

now I need to figure out how to wait until the document is printed and then have it delete the contents of the folder, what would be your recommendations ?

Sorry about that Rod-IT i will make sure to do this next time !

Why not determine how long it takes a large PDF to spool, then put a wait in for around that time before deleting.

If a PDF on the large size takes 5 minutes to spool, delete the PDFs in the folder after 10 minutes?

Nothing to be sorry about, it simply makes it easier to read for everybody, including your, when people reply.

Glad it worked. Make sure that you mark best and helpful answers. :slight_smile:

I am not sure that there is a cmdlet/command to print and wait till printing is complete. You could just finish the printing, then wait, say, 10 minutes then delete.

A suggestion for future improvement - instead of deleting, put them in a ‘printed $date’ folder, then the next time you run the script, it deletes folders older than X days. That way a printer failure doesn’t leave you with a copy to print.

2 Spice ups