title: Powershell script to run a .bat file when a file is added to a folder
body: <p>I'd like to monitor a Windows 7 folder and have a .bat file run when any new files are added to the folder. It seems like I should be able to do this using powershell, which is installed on the computer. </p>
  
  <p>I've read some answers like <a href="https://stackoverflow.com/questions/5923802/execute-batch-file-when-a-new-file-is-added-to-a-folder">this one</a> but I'm not able to get anything to work yet by modifying what I see. Details:</p>
  
  <p>Folder to monitor:</p>
  
  <pre><code>c:\aaa\bbb\monitorThis
  </code></pre>
  
  <p>Batch file to run whenever an .htm file is added to the monitored folder:</p>
  
  <pre><code>c:\aaa\bbb\runA.bat
  </code></pre>
  
  <p>Powershell script file:</p>
  
  <pre><code>c:\aaa\bbb\folderWatcher.ps1
  </code></pre>
  
  <p>Can someone describe what the content of folderWatcher.ps1 should look like, including the line containing the command to run the .bat file, registering and unregistering the event, and so on? </p>
  
  <p>Also, is right-clicking the .ps1 file and selecting "Run with PowerShell" the way to start the monitoring, and if so, how do you stop it?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>As requested, here is what I have so far for folderWatcher.ps1, but it's just a start, from ideas I've seen:</p>
  
  <pre><code>$folder = "c:\aaa\bbb\toConvert"
  $filter = "*.*"
  
  $fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubDirectories=$false
  NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
  }
  
   $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
  
    Start-Process cmd -ArgumentList "/c runA.bat" -WorkingDirectory "C:\aaa\bbb"
  }
  </code></pre>
  
  <p>Note: re filtering, I don't care what kind of file is added, since we will only be putting .htm files in that folder, so anything added to it I want to trigger the .bat.</p>
  
  <p><strong>UPDATE II</strong></p>
  
  <p>I tried the code from Dennis below but I get nothing. I just double-checked all my paths to be sure they were the equivalent of what he has. I also just made a new test version with simpler paths so I can post exactly what I have without having to anonymize:</p>
  
  <pre><code> $folder = 'C:\Developer\psTest' # Enter the root path you want to monitor. 
   $filter = '*.htm'  # You can enter a wildcard filter here. 
  
   $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property  @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
  
   Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
   $name = $Event.SourceEventArgs.Name 
   $changeType = $Event.SourceEventArgs.ChangeType 
   $timeStamp = $Event.TimeGenerated 
   Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
   cmd.exe /c 'C:\Developer\psTest\runAnt.bat'
   }
  </code></pre>
  
  <p>To be clear what I'm doing: </p>
  
  <p>I have a file now called <code>C:\Developer\psTest\FolderWatcherTest.ps1</code> that contains the code directly above.</p>
  
  <p>When I right-click it and select Run with PowerShell, a console window flashes with some text but it's too fast to read before it closes.</p>
  
  <p>When I then drag an .htm file into <code>C:\Developer\psTest</code>, nothing happens. </p>
  
  <p>I put the unregister code into a file called FolderWatcherStop.ps1, and when I click that, the console flashes with some red text, again too quick to read, then it closes.</p>
  
  <p>What am I doing wrong? Something I'm sure. </p>
  
  <p><strong>UPDATE III</strong></p>
  
  <p>Following Dennis's advice I got this working. This took modifying the batch file a little to include the full path of the ANT build I want to run, but it works.</p>
  
  <p>Note: I think I just got why the trigger is repeating, will update.</p>