Printing New Files In Directory

I am brand new to Powershell. I have only used some VB with excel apps and that is about the most I know about programming. I’m also new to this forum!

What I would like to do is monitor many directories under 1 path and print new files that are created anytime between 1:00AM and 6:00AM to my default printer. Is this possible with powershell? I really need help with this and would really
appreciate the help. Thank you in advance!

It is absolutely possible. Assuming that the files you are monitoring have a print option in their context menu it should be very easy. Look at the Get-ChildItem (alias dir) and Start-Process. With Get-ChildItem, look a the -filter parameter, and with start-process look at the -verb parameter.

Thank you for the reply! The files are all .HTM.

Here is some code I was playing around with, trying to understand whats going on. Found this on the web

    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\User\Desktop\EOSRPrint"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $false  

    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "C:\Users\User\Desktop\log.txt" -value $logline
    $created = Register-ObjectEvent $watcher "Created" -Action $action
    $changed = Register-ObjectEvent $watcher "Changed" -Action $action
    $deleted = Register-ObjectEvent $watcher "Deleted" -Action $action
    $renamed = Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}

I’m really not sure what I’m doing. This is so new to me but looks promising!

Ok so you want this as an always running processes, or will you be starting it and then it prints the files from that timeframe?

Always process - as long as I have the powershell window open if that is possible. I guess if I were to need to run it 1 time would also be useful but not main objective.

As you help me I’m playing around with commands. Took me a long time to figure out (google!) that I have to use ’ around command to change directories… commands are like dos commands with some differences. I feel like I have a new toy!

will the name of the file ever repeat?

Yes, the file name is saved with the date in the name and have the word NIGHT.HTM.

So *-YYYYMMDD-NIGHT.HTM will be the format every time.

Playing with that monitoring code, looks like the newest file during that time period with the word NIGHT.HTM in it would work. Date being in the name might not have anything to do with finding the right 1. Just the latest in the folder during that time period will work.

So you may get multiple files a night so maybe at 1 o’clock you get file-20151109-NIGHT.HTM, and then at 2 o’clock you get file-20151109-NIGHT.HTM, etc. etc., and each time the new file overwites the previous file?

or does the * in front of your file name indicate that there is something else to always make the file name unique, but you only get one per night so the name never repeats.


During the time of 1am to 6am there will only be 1 new file created during this time frame.

In each folder under the top.

Ok so something like this were the file name does repeat, but it’s under a different folder?


During the time of 1am to 6am there will only be 1 new file created during this time frame.

In each folder under the top.

Will the file name matter if we look for newest file? I have no idea, just trying to make it easy.

yes is maters because of how the events get generated with the file system watcher. Unfortunately when a file is created, it does not always generate just one creation event. Different applications may generate multiple creation events for the creation of one file. As such you have to add logic so the script knows when it has already printed a file. Using the file name is the most logical for this purpose if the file name is unique. If it is not, then possibly using a combination of parent folder and file name to uniquely identify the file will work. If you don’t add this logic, a single file could get printed multiple times.

Of course this is only an issue because of the request to always be running and printing based off of the event.

It would be much easier to write a script that kicks off with task scheduler at say 6:05 AM and looks for all file in the directory structure between 1:00 and 6:00 the current day and print those.

Ok, I can do that for sure. I’ll set up a windows task run it.

Start-Process -FilePath 'p:\filepath\folder1\CON-20151108-NIGHT.HTM' –Verb Print -PassThru | %{sleep 10;$_} | kill 

This line works! Problem is the program that is saving the files is saving them with the date in the name. How do I fix the command to print only the file with the current date?

Current date formatted like *YYYYMMDD-NIGHT.HTM

If that works for you, then this should work for you.

$Path = "F:\Temp\Powershell\part\*.htm"
$StartDate = Get-Date -Hour 1 -Minute 0 -Second 0 -Millisecond 0
$EndDate = Get-Date -Hour 6 -Minute 0 -Second 0 -Millisecond 0
Get-ChildItem -Path $Path -Recurse | 
Where-Object {$_.CreationTime -gt $StartDate -AND $_.CreationTime -lt $EndDate} |
ForEach-Object {
    Start-Process -FilePath ($_.FullName) –Verb Print -PassThru | %{sleep 10;$_} | kill

I was having a challenge with Start-Process with the -verb parameter because IE would just show the print dialog box, but not actually submit the print job. Was looking for an alternative, and not having good luck.

I was getting an error.

“Get-Date : A parameter cannot be found that matches parameter name ‘millisecond’.”

I removed the -millisecond 0 and it worked… so I’m not sure why it is giving an error.

What it looks like, I will need to make 1 of these for each folder and set up the windows scheduler to run them. That is what you were thinking correct?

“Get-Date : A parameter cannot be found that matches parameter name ‘millisecond’.”

That means you are running PowerShell 2.0 or older. You should probably upgrade. PowerShell 5.0 is in product preview. Soon to be fully released.

What it looks like, I will need to make 1 of these for each folder and set up the windows scheduler to run them. That is what you were thinking correct?

Nope, the get-childitem cmdlet is using the -recurse parameter, so you should be able to specify your parent folder and it will recursively look through all of your child folders.

Ok, I will upgrade my version. Keep in mind I just happen to run across powershell about a week ago if that, so I am a noob! I can see the power and I will continue to play with it for sure.

I do have 1 more question - is there any way to exclude folders under the parent folder?
There are more folders that need to be printed vs a few that don’t. Would be nice to be able to exclude a few folders and not waste paper.

yes, check out the -exclude parameter of the get-childitem cmdlet or piping into where-object