title: How to detect Folder change (renaming in particular) inside a folder using Powershell file watcher
body: <p>I am using powershell to detect and record a folder change (created, renamed, deleted etc) inside a folder.
  This parent folder receives subfolders from another location in format <strong>TEMP_XYZ</strong>. Once this folder is copied to this parent folder, the process automatically renames it to <strong>XYZ</strong> (Removed suffix TEMP_)</p>
  <p>This change (rename) had to be detected and recorded in a log file as</p>
  <pre><code>\\test\folderwatch\XYZ was Renamed at 7/28/2021 2:03:00 PM
  
  Folder TEMP_XYZ was renamed to XYZ
  </code></pre>
  <p>However, I am not able to achieve this as the code below only works on files. (txt,bmp, zip etc)</p>
  <p>Any help is appreciated.</p>
  <p>Code:</p>
  <pre><code># specify the path to the folder you want to monitor:
  $Monitorpath =&quot;\\test\folderwatch&quot;
  $Path = $Monitorpath
  
  # specify which files you want to monitor
  $FileFilter = '*'  
  
  # specify whether you want to monitor subfolders as well:
  $IncludeSubfolders = $true
  
  # specify the file or folder properties you want to monitor:
  $AttributeFilter = [IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite 
  
  try
  {
    $watcher = New-Object -TypeName System.IO.FileSystemWatcher -Property @{
      Path = $Path
      #Filter = $FileFilter
      IncludeSubdirectories = $IncludeSubfolders
      NotifyFilter = $AttributeFilter
    }
  
    $action = {
      # change type information:
      $details = $event.SourceEventArgs
      $Name = $details.Name
      $FullPath = $details.FullPath
      $OldFullPath = $details.OldFullPath
      $OldName = $details.OldName
      $ChangeType = $details.ChangeType
      $Timestamp = $event.TimeGenerated
      $LogDate = Get-Date -format &quot;dd-MMMM-yy&quot;
      
      # save information to a global variable for testing purposes
      # so you can examine it later
      # MAKE SURE YOU REMOVE THIS IN PRODUCTION!**************************DO NOT USE FOR PROD**************************
      $global:all = $details
      
      $text = &quot;{0} was {1} at {2}&quot; -f $FullPath, $ChangeType, $Timestamp
      Write-Host &quot;&quot;
      Write-Host $text -ForegroundColor DarkYellow
      Add-content &quot;\\test\folder_watch_logs\watchlog_$LogDate.txt&quot; -value $text
  
      switch ($ChangeType)
      {
        'Changed'  { &quot;CHANGE&quot; }
        'Created'  { &quot;CREATED&quot;}
        'Deleted'  { &quot;DELETED&quot;
  
          Write-Host &quot;Deletion Handler Start&quot; -ForegroundColor Gray
          Start-Sleep -Seconds 4  
          Write-Host &quot;Deletion Handler End&quot; -ForegroundColor Gray
        
        }
        'Renamed'  { 
          # this executes only when a file was renamed
          $text = &quot;Folder {0} was renamed to {1}&quot; -f $OldName, $Name
          Write-Host $text -ForegroundColor Yellow
          Add-content &quot;test\folder_watch_logs\watchlog_$LogDate.txt&quot; -value $text
        }
          
        # any unhandled change types surface here:
        default   { Write-Host $_ -ForegroundColor Red -BackgroundColor White ;
        Add-content &quot;test\folder_watch_logs\watchlog_$LogDate.txt&quot; -value $_ }
      }
    }
  
    $handlers = . {
      Register-ObjectEvent -InputObject $watcher -EventName Changed  -Action $action 
      Register-ObjectEvent -InputObject $watcher -EventName Created  -Action $action 
      Register-ObjectEvent -InputObject $watcher -EventName Deleted  -Action $action 
      Register-ObjectEvent -InputObject $watcher -EventName Renamed  -Action $action 
    }
  
    # monitoring starts now:
    $watcher.EnableRaisingEvents = $true
    $LogDate = Get-Date -format &quot;dd-MMMM-yy&quot;
    Write-Host &quot;Watching for changes to $Path&quot;
    Add-content &quot;test\folder_watch_logs\watcherstatus.txt&quot; -value &quot;Watching for changes to $Path&quot;
  
    # since the FileSystemWatcher is no longer blocking PowerShell
    # we need a way to pause PowerShell while being responsive to
    # incoming events. Use an endless loop to keep PowerShell busy:
    do
    {
      Wait-Event -Timeout 1
  
      # write a dot to indicate we are still monitoring:
      #Write-Host &quot;.&quot; -NoNewline
          
    } while ($true)
  }
  finally
  {
    # stop monitoring
    $watcher.EnableRaisingEvents = $false
    
    # remove the event handlers
    $handlers | ForEach-Object {
      Unregister-Event -SourceIdentifier $_.Name
    }
    
    $handlers | Remove-Job
    
    # properly dispose the FileSystemWatcher:
    $watcher.Dispose()
    $LogDate = Get-Date -format &quot;dd-MMMM-yy&quot;
    Write-Warning &quot;Event Handler disabled, monitoring ends.&quot;
    Add-content &quot;test\folder_watch_logs\watcherstatus.txt&quot; -value &quot;Event Handler disabled, monitoring ends.&quot;
  }
  </code></pre>
  <p>//Jagbir</p>