It would appear that you are getting multiple events. Add a test-path and only call Excel if the file exists.
### If testing in ISE, unregister prior events
Get-EventSubscriber |Unregister-Event
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\temp"
$watcher.Filter = "*.txt"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = {
$path = $Event.SourceEventArgs.FullPath
"$(Get-Date), Event triggered for {0}" -f $Event.SourceEventArgs.FullPath | write-host
$changeType = $Event.SourceEventArgs.ChangeType
"$(Get-Date), $changeType, $path" | Write-Host
if (test-path -Path $path) {
Write-Host "The file exists, here are it's contents."
$data = Get-Content $path
Write-Host $data
Write-Host "You could call excel here to process the file."
Write-Host "I am just going to delete it to indicate that it was processed."
Remove-Item $path
} else {
Write-Host "File not found. We already processed it."
}
Write-Host "Event complete."
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
### Register-ObjectEvent $watcher "Created" -Action $action
Register-ObjectEvent $watcher "Changed" -Action $action
### Register-ObjectEvent $watcher "Deleted" -Action $action
### Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}