Powershell FileSystemWatcher

This script uses the .net FileSystemWatcher class to subscribe to 'create', 'change' and 'delete' events on files or folders in the NTFS filesystem.It can be used to monitor a folder or folders, and can be modified to perform any action upon the triggering of these events. 

 
 
 
 
 
4.5 Star
(21)
14,208 times
Add to favorites
Storage
12/8/2011
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • File Change always comes twice !!!
    1 Posts | Last post March 24, 2016
    • now is every day just ne new log file created yyyy_MM_dd.txt fit only change problem I have yet. !! the create (file is box folder XML) is green written an entry and additional 2x change what I want !!! when I change the file in the folder XML should there be change message which comes but twice !! if I file delete as desired only 1x red message delete reported! that's OK.
      I do not want is to create the change message in and not always 2 times .. written somewhere too much I think ...
  • Hi, how do I get the script to run automatically?
    2 Posts | Last post March 23, 2016
    • I would like that if an xml file comes in, automatically log.txt is created! currently I need to start powershell manually to create a log.txt
      
      my script:
      
      Get-ChildItem -Path 'D:\Kemal test xml\XML' | SELECT CreationTime, name | Format-Table -AutoSize | Out-File -filepath ('D:\Kemal test xml\Logfiles\log'+((Get-Date).ToString("yyyy_MM_dd"))+'.txt')
      
      an result is:
      
      log2016_03_23.txt (text Document)
      
      
      
    • now everyone is entry 3 times written to the log.txt! 2x change and 1x create ...
      
      it should make only 1x create or 1x change or 1x delete ...!!
      
      plz help!! 
      i use the script FileSystemWatcher!
  • How to add a file move / rename
    1 Posts | Last post November 23, 2015
    • Thank you for your script, I'm wondering how I could add one piece of functionality to it, 
      
      When the file created goes off, that it would copy to another directory, and rename the existing file to yyyyMMdd format. 
      
      I don't know where to put that in, or if I just need to create something new? I love the script though and it's working flawlessly for updating that things are changing. 
      Thank you. 
  • new folders within folders
    1 Posts | Last post November 10, 2015
    • Hi.
      
      If I create new folders within the watched folder, it doesn't keep track of any new files.
      
      Is this expected or am I doing something wrong?
  • Awesome Script
    1 Posts | Last post September 03, 2015
    • This is an amazing script.
  • Run as service/batch file
    1 Posts | Last post September 03, 2015
    • I have modified the script to my liking and now I try to run it as a service or start it using a batch file. Both do not work. When running as a service and creating/updating/deleting a file the service stops. No files are logged. When running as batch file only 1 item is logged. After the first file nothing gets logged.
      
      Any ideas on how to run this script in batch or as a service?
  • How to monitor by excluding a few files in a folder.
    1 Posts | Last post July 20, 2015
    • How to monitor by excluding a few files in a folder.
  • monitor sub-folders
    2 Posts | Last post February 25, 2015
    • can it monitor files inside the sub-folder of the root path?
    • please disregard my question above .. just tested it and works.
      great script - thank you!
  • as service
    5 Posts | Last post February 17, 2015
    • Hi,
      
      your script is great.
      I ask to you if it's possible enable it permanent or as service.
      
      thank a lot
    • No, unfortunately it is a script, and as such, must be running all the time.  You could write a service in C#, using the same FilesystemWatcher class.
    • PowerGUI ( http://powergui.org/index.jspa ) very successfully compiled my modified version of this script to an exe, there is also an option to compile to service but I have not properly tested it yet. It might work for you.
      
      PS, thanks to Big Teddy for the useful code!
    • I agree, this script is great! 
      I compiled the script as a service but it no longer works. I do have to be logged on. Any other ideas out there? Thanks! 
    • you can do that by :
      
      1- create a batch file "filesystemwatcher.bat" 
      2- the contents of this batch will be : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -file "path to your ps script"
      3- add this batch to the startup scripts of the server's local policy . or you can use a group policy . gpedit.msc -> computer configuration -> windows settings -> scripts (startup /shutdown) -> startup
      4- after restarting the server , the powershell script will run in the background .
      
      that is ALL .
  • Copy-Item on 'Changed'
    1 Posts | Last post May 28, 2013
    • Hi Bigteddy - first of all thanks for the nice script!
      
      I have a question though:
      I'm trying to do a Copy-Item action upon triggering of an event but this stops after one or just a few copy actions.
      If I leave the Copy-Item out and just use the Write-Host everything is fine.
      
      I've done the same with the 'Created' event and that seems to be working ok.
      
      Any Ideas?
1 - 10 of 19 Items