Saturday, April 26, 2014

PowerShell example to monitor files and directories for any changes using FileSystemWatcher

<# Create delegates that will be used when a file is altered and when when it is renamed #>
$updated = { Write-Host "File: " $EventArgs.FullPath " " $EventArgs.ChangeType ;$global:UpdateEvent = $EventArgs}

$Renamed = { Write-Host "File: " $EventArgs.OldFullPath " renamed to "$EventArgs.FullPath ; $global:RenameEvent = $EventArgs}

<# Create a fileWatcher that will monitor the directory and add its attributes#>
$fileWatcher = new-object System.IO.FileSystemWatcher
$fileWatcher.Path = "C:\Junk"
$fileWatcher.Filter = "*.txt";
$fileWatcher.IncludeSubdirectories = $true
$fileWatcher.NotifyFilter = [System.IO.NotifyFilters]::LastAccess `                      
                            [System.IO.NotifyFilters]::LastWrite, `
                            [System.IO.NotifyFilters]::FileName `

<# If a delegate has already been added to the FileWatchers for that event remove it and add the new one. #>
if($ChangedEvent) {$ChangedEvent.Dispose()}
$ChangedEvent = Register-ObjectEvent $fileWatcher "Changed" -Action $updated

if($CreatedEvent) {$CreatedEvent.Dispose()}
$CreatedEvent = Register-ObjectEvent $fileWatcher "Created" -Action $updated

if($DeletedEvent) {$DeletedEvent.Dispose()}
$DeletedEvent = Register-ObjectEvent $fileWatcher "Deleted" -Action $updated

if($RenamedEvent) {$RenamedEvent.Dispose()}
$RenamedEvent = Register-ObjectEvent $fileWatcher "Renamed" -Action $Renamed

$fileWatcher.EnableRaisingEvents = $true

#To stop monitoring the text files in your specified directory run the script bellow.
if($RenamedEvent) {$RenamedEvent.Dispose()}
if($DeletedEvent) {$DeletedEvent.Dispose()}
if($CreatedEvent) {$CreatedEvent.Dispose()}
if($ChangedEvent) {$ChangedEvent.Dispose()}

     c# style go add delegates to your filewatcher
     fileWatcher.Changed += new FileSystemEventHandler(Wathcer_Changed);
     fileWatcher.Created += new FileSystemEventHandler(Wathcer_Changed); 
     fileWatcher.Deleted += new FileSystemEventHandler(Wathcer_Changed);

     static void Wathcer_Changed(object sender, FileSystemEventArgs e)
          Console.WriteLine("Change Type = {0}, Path = {1}", e.ChangeType, e.FullPath);


  1. Thanks for the post. I know it was a while ago that you created it, but I was wondering if you might be able to shed some light on an issue I am seeing?

    When changing the contents of a file (simple add text and save) in Visual Studio 2015, it is firing the "deleted" event, but if I change in notepad it reflects the change event.

    Do you know what might be happening in VS that makes this happen? I tried a google, but I couldn't get search terms that gave me anything useful.


    1. Found this article.

  2. Hi I am using your script. But the changes event is triggered twice. Is there a way I can fix that.

  3. sorry for the delay, if you ran it multiple times you will be multiple monitors on the directory. You have to delete the FileSystemWatchers. The commented code at the end shows how to delete the FileSystemWatcher .