PowerShell Script to Monitor File Changes Easily
System.IO.FileSystemWatcher class in PowerShell to monitor file changes by creating a watcher object, setting its path and filters, and registering event handlers with Register-ObjectEvent.Examples
How to Think About It
Algorithm
Code
Add-Type -AssemblyName System.IO.FileSystemWatcher $folder = 'C:\Temp' $filter = '*.*' $watcher = New-Object System.IO.FileSystemWatcher $folder, $filter $watcher.EnableRaisingEvents = $true Register-ObjectEvent $watcher Created -SourceIdentifier FileCreated -Action { Write-Host "File created: $($Event.SourceEventArgs.Name)" } Register-ObjectEvent $watcher Changed -SourceIdentifier FileChanged -Action { Write-Host "File changed: $($Event.SourceEventArgs.Name)" } Register-ObjectEvent $watcher Deleted -SourceIdentifier FileDeleted -Action { Write-Host "File deleted: $($Event.SourceEventArgs.Name)" } Write-Host "Monitoring changes in $folder. Press Enter to exit." [Console]::ReadLine() | Out-Null Unregister-Event -SourceIdentifier FileCreated Unregister-Event -SourceIdentifier FileChanged Unregister-Event -SourceIdentifier FileDeleted $watcher.Dispose()
Dry Run
Let's trace monitoring a file creation event in C:\Temp through the code
Create watcher
Watcher set to monitor C:\Temp for all files (*.*)
Enable events
Watcher starts listening for Created, Changed, Deleted events
File created event
User creates 'test.txt' in C:\Temp, event triggers, script prints 'File created: test.txt'
| Step | Event | File Name | Output |
|---|---|---|---|
| 1 | Start | - | Monitoring changes in C:\Temp. Press Enter to exit. |
| 2 | Created | test.txt | File created: test.txt |
Why This Works
Step 1: FileSystemWatcher object
The FileSystemWatcher object watches a folder for file changes and raises events when something happens.
Step 2: Registering events
Using Register-ObjectEvent, we tell PowerShell what to do when files are created, changed, or deleted.
Step 3: Keeping script alive
The script waits for user input to keep running so it can keep listening for file changes.
Alternative Approaches
while ($true) { $files = Get-ChildItem -Path 'C:\Temp' # Compare with previous list to detect changes Start-Sleep -Seconds 5 }
Register-WmiEvent -Class Win32_DirectoryChangeEvent -SourceIdentifier DirChange -Action {
Write-Host "Directory changed"
} -MessageData 'C:\Temp'Complexity: O(1) per event time, O(1) space
Time Complexity
The watcher reacts to events instantly without scanning the whole folder, so time per event is constant.
Space Complexity
Uses minimal memory to store watcher and event handlers; no large data structures needed.
Which Approach is Fastest?
FileSystemWatcher is faster and more efficient than polling because it uses OS notifications.
| Approach | Time | Space | Best For |
|---|---|---|---|
| FileSystemWatcher | O(1) per event | O(1) | Real-time monitoring with low resource use |
| Polling with Get-ChildItem | O(n) per check | O(n) | Simple scripts where real-time is not critical |
| WMI Events | O(1) per event | O(1) | Advanced monitoring with system integration |