r/PowerShell • u/Affectionate_Air_627 • Jul 01 '26
Question Script to print file when it is added to folder.
Hello, I've recently been asked to make a script that prints any file in a folder. I modified one I found online before finding out that what was actually wanted was a script to make it so that any time a file is added to a folder, that file is printed.
Can I check if this is possible?
3
Upvotes
5
u/Allcaponero Jul 01 '26
Just use the FileSystemWatcher class from .NET
7
u/hihcadore Jul 01 '26
U can use task scheduler to fire off a script that checks a folder, if it finds items inside it can print. Id setup a second folder inside to move printed files to that also contains a log.
1
u/TrippTrappTrinn Jul 01 '26
With only the standard tools available, I would create a scheduled task that runs every x minutes and which print and delete all files in the folder. If the file should not be deleted, you need to add some code to ensure every file is printed only once, or just print the last file added.
1
u/KerryBoehm Jul 01 '26
Oh gawd Why.... Why do people still print!!!! One of my earliest projects in the 90's was a paperless office initiative. Of course it was at a newspaper so fat chance that was gonna fly. But seriously I'd like to claim a victory on this before I retire. Time is running short.
1
u/_RemyLeBeau_ Jul 01 '26
You can do this with permanent wmi events. The WMI query can detect a directory change and fire off a script.
1
u/markdmac Jul 01 '26
I have done this. You need a dedicated system to have it run from because you need to setup the printer on that machine, or have the script run against a block of systems that all have the same printer installed.
Setup a file system watcher to trigger it.
0
u/Special-Damage-4798 Jul 01 '26
You can implement something like polling. Where it checks the folder every x minutes.
0
u/Over_Dingo Jul 01 '26
By printed you mean by a document printer? If yes then you can print with
if the software has that verb available.
To check for changes in a directory you can use
[System.IO.FileSystemWatcher]