title: Powershell - Monitoring folder Content
body: <p>I would like to be able to monitor the changes in a folder for a short period of time when a lot of files will be created and other changes will be made.</p>
  
  <p>The code below is working but doesn't catch all the changes.</p>
  
  <pre><code>$folder = ’C:\Data’
  $timeout = 1000
  $FileSystemWatcher = New-Object System.IO.FileSystemWatcher $folder
  Write-Host ”Press CTRL+C to abort monitoring $folder”
  while ($true) {
  $result = $FileSystemWatcher.WaitForChanged(‘all’, $timeout)
  if ($result.TimedOut -eq $false)
  {
  Write-Warning (‘File {0} : {1}’ -f $result.ChangeType, $result.name)
  }
  }
  Write-Host ’Monitoring aborted.’
  </code></pre>
  
  <p>If I use this on <code>C:\Data</code> it works <strong>but</strong>:</p>
  
  <p>I create a .txt and it says 2 times <code>new text document.txt</code>. 
  Then I fill the name for the new txt and it outputs it 3 times. Or the other way around. </p>
  
  <p>Please see below the output of </p>
  
  <ul>
  <li>creating a <code>hello.txt</code> in my folder</li>
  <li>creating a new folder with the name <code>HiThere</code> and  </li>
  <li>then renaming <code>hello.txt</code> to <code>someTxt.txt</code></li>
  <li>then deleting them both</li>
  </ul>
  
  <p><strong>output:</strong></p>
  
  <pre><code>`Press CTRL+C to abort monitoring C:\Data
  WARNING: File Created : New Text Document.txt
  WARNING: File Changed : New Text Document.txt
  WARNING: File Changed : New Text Document.txt
  WARNING: File Changed : hello.txt
  WARNING: File Created : New folder
  WARNING: File Renamed : HiThere
  WARNING: File Renamed : someTxt.txt
  WARNING: File Changed : someTxt.txt
  WARNING: File Changed : someTxt.txt
  WARNING: File Changed : someTxt.txt
  WARNING: File Deleted : someTxt.txt
  WARNING: File Deleted : HiThere`
  </code></pre>
  
  <p><strong>More problems</strong>: If I use this on a newtwork drive then not all the changes are being catched. (And this would be the point of this script, to monitor a folder from a mapped drive).</p>
  
  <p><em>Test the code on your machine</em> by only changing the folder path. </p>
  
  <p>Using Powershell ISE 3.0</p>