Powershell and System.IO.FileSystemWatcher


I am new to PowerShell and I am trying to use the System.IO.FileSystemWatcher to monitor the presence of a file in a specified folder. However, as soon as the file is detected I want to stop monitoring this folder immediately and stop the FileSystemWatcher. The plan is to incorporate the PowerShell script into a SQL Agent to enable users to restore their own databases. Basically I need to know the command to stop FileSystemWatcher from monitoring as soon as one file is found. Here is the script so far.


This will unregister the event. You will probably want to add this to the $action.

Do note that if there are events in the queue they will still be fired.

This might help too.


Powershell and System.IO.FileSystemWatcher by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply