title: FileSystemWatcher works in the PowerShell ISE but not when run
body: <p>I want to monitor a folder and move files that match certain criteria, so I'm trying to use the FileSystemWatcher.</p>
  
  <p>I have a function that will be called with each new file:</p>
  
  <pre><code>function ProcessFile()
  {
      param ([string]$filename)
      Write-Host "Processing file '$filename' to $destination"
  }
  </code></pre>
  
  <p>And then I set up a FSW:</p>
  
  <pre><code>Write-Host "Watching $source for new files..."
  $fsw = New-Object IO.FileSystemWatcher $source, $filter -Property @{IncludeSubdirectories = $false; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
  
  Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action 
  {
      ProcessFile $Event.SourceEventArgs.FullPath 
  }
  </code></pre>
  
  <p>That works fine when I run it from the ISE, and any files I drop into the watched folder are correctly tracked, but if I start a PowerShell window and run the script with .\FileWatch.ps1 then nothing happens.</p>
  
  <p>I see the "watching ..." message, but never see a "processing..." message</p>
  
  <p>Here's the full script that works in the ISE but not in a shell...</p>
  
  <pre><code>$source = 'D:\Dev\PowerShell\FileWatch\Test\Source'
  $filter = '*.*'
  $destination = 'D:\Dev\PowerShell\FileWatch\Test\Found\'
  
  function ProcessFile()
  {
      param ([string]$filename)
      Write-Host "Processing file '$filename' to $destination"
  }
  
  Write-Host "Watching $source for new files..."
  $fsw = New-Object IO.FileSystemWatcher $source, $filter -Property @{IncludeSubdirectories = $false; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
  Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
      ProcessFile $Event.SourceEventArgs.FullPath 
  }
  </code></pre>