{"id":11311,"date":"2026-05-16T01:01:01","date_gmt":"2026-05-16T01:01:01","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/how-to-monitor-folder-changes-using-powershell-and-filesystemwatcher\/"},"modified":"2026-05-16T01:01:01","modified_gmt":"2026-05-16T01:01:01","slug":"how-to-monitor-folder-changes-using-powershell-and-filesystemwatcher","status":"publish","type":"post","link":"http:\/\/cheapwindowsvps.com\/blog\/how-to-monitor-folder-changes-using-powershell-and-filesystemwatcher\/","title":{"rendered":"How to Monitor Folder Changes Using PowerShell and FileSystemWatcher"},"content":{"rendered":"<p>Monitoring a specific folder or file for changes can be efficiently achieved using PowerShell with the built-in .NET <code>FileSystemWatcher<\/code> class. This approach allows for real-time detection of changes such as creation, deletion, renaming, or modification of files within a specified directory. By utilizing the <code>FileSystemWatcher<\/code>, you can automate actions in response to these events, such as logging information or executing custom scripts.<\/p>\n<p>To set up <code>FileSystemWatcher<\/code>, start by creating an instance of the class:<\/p>\n<pre><code class=\"language-powershell\">$fs_watch = New-Object System.IO.FileSystemWatcher<\/code><\/pre>\n<p>Next, specify the directory you wish to monitor:<\/p>\n<pre><code class=\"language-powershell\">$fs_watch.Path = &quot;C:RepoConfig&quot;<\/code><\/pre>\n<p>If you want to include subdirectories in your monitoring, set the following property:<\/p>\n<pre><code class=\"language-powershell\">$fs_watch.IncludeSubdirectories = $true<\/code><\/pre>\n<p>Then, enable event generation for filesystem changes:<\/p>\n<pre><code class=\"language-powershell\">$fs_watch.EnableRaisingEvents = $true<\/code><\/pre>\n<p>Define the action to be executed when changes occur in the monitored folder:<\/p>\n<pre><code class=\"language-powershell\">$reg_action = {    $changeType = $Event.SourceEventArgs.ChangeType    $path = $Event.SourceEventArgs.FullPath    &quot;$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') | $changeType | $path&quot; | Out-File -FilePath &quot;$env:USERPROFILEwatch.log&quot; -Append}<\/code><\/pre>\n<p>Subscribe to the event types you want to track\u2014specifically, <strong>Created<\/strong>, <strong>Changed<\/strong>, <strong>Deleted<\/strong>, and <strong>Renamed<\/strong>:<\/p>\n<pre><code class=\"language-powershell\">Register-ObjectEvent $fs_watch Created -Action $reg_action -SourceIdentifier FSCreateRegister-ObjectEvent $fs_watch Changed -Action $reg_action -SourceIdentifier FSChangeRegister-ObjectEvent $fs_watch Deleted -Action $reg_action -SourceIdentifier FSDeleteRegister-ObjectEvent $fs_watch Renamed -Action $reg_action -SourceIdentifier FSRename<\/code><\/pre>\n<p>The <code>FileSystemWatcher<\/code> will operate as long as the <code>PowerShell.exe<\/code> process is active. However, if you run the code through a script file, consider implementing an infinite wait at the end:<\/p>\n<pre><code class=\"language-powershell\">while ($true) { sleep 10 }<\/code><\/pre>\n<p>This construct is particularly useful when the monitoring script is executed via Task Scheduler or set up as a Windows service.<\/p>\n<p>To view the log file with real-time updates, execute:<\/p>\n<pre><code class=\"language-powershell\">Get-Content -Path $env:USERPROFILEwatch.log -wait<\/code><\/pre>\n<p>If you need to unregister from the events, you can use:<\/p>\n<pre><code class=\"language-powershell\">Unregister-Event -SourceIdentifier FSCreateUnregister-Event -SourceIdentifier FSChangeUnregister-Event -SourceIdentifier FSDeleteUnregister-Event -SourceIdentifier FSRename<\/code><\/pre>\n<p>If you&#8217;re interested in tracking only specific file types, apply a filter at the start of the script:<\/p>\n<pre><code class=\"language-powershell\">$filter = &quot;*.ini&quot;$fs_watch.Filter = $filter<\/code><\/pre>\n<p>You can also specify the types of changes you wish to monitor using the <strong>NotifyFilter<\/strong> property:<\/p>\n<pre><code class=\"language-powershell\">$fs_watch.NotifyFilter = [System.IO.NotifyFilters]::FileName -bor [System.IO.NotifyFilters]::Size -bor [System.IO.NotifyFilters]::Security<\/code><\/pre>\n<p>For more complex event handling, such as logging old and new file names during a renaming event, customize the action accordingly:<\/p>\n<pre><code class=\"language-powershell\">Register-ObjectEvent $fs_watch Renamed -SourceIdentifier FSRename -Action {    $details = $Event.SourceEventArgs    $timeStamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'    $logMessage = &quot;$timeStamp | Renamed | From: $($details.OldFullPath) To: $($details.FullPath)&quot;    $logMessage | Out-File -FilePath $env:USERPROFILEwatch.log -Append}<\/code><\/pre>\n<p>To ensure that events are not lost, especially in scenarios with a high volume of changes, you may consider increasing the internal buffer size:<\/p>\n<pre><code class=\"language-powershell\">$fs_watch.InternalBufferSize = 65536<\/code><\/pre>\n<p>Alternatively, handling actions in a separate background process using the <code>Start-Job<\/code> cmdlet is recommended for isolating execution from the watcher.<\/p>\n<p>In essence, this guide illustrates how to implement a straightforward file system monitoring solution using PowerShell to comprehensively track file events and perform automated responses accordingly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Monitoring a specific folder or file for changes can be efficiently achieved using PowerShell with the built-in .NET FileSystemWatcher class. This approach allows for real-time detection of changes such as creation, deletion, renaming, or modification of files within a specified directory. By utilizing the FileSystemWatcher, you can automate actions in response to these events, such [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":11312,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117,99,121],"tags":[],"class_list":["post-11311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-windows-11","category-windows-server-2025"],"_links":{"self":[{"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/11311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=11311"}],"version-history":[{"count":0,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/11311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/11312"}],"wp:attachment":[{"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=11311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=11311"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=11311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}