poweshell监控文件夹变化
# Define the folder path to monitor $folder = 'C:\Downloads' # Define the filter for the type of files to monitor $filter = '*.*' # Define the options for the file system watcher $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $folder $watcher.Filter = $filter $watcher.IncludeSubdirectories = $true $watcher.EnableRaisingEvents = $true # Define the action to take when a file is created, changed, or deleted $action = { $path = $Event.SourceEventArgs.FullPath $changeType = $Event.SourceEventArgs.ChangeType $timeStamp = $Event.TimeGenerated Write-Host "The file '$path' was $changeType at $timeStamp" } # Add the action to the file system watcher $created = Register-ObjectEvent $watcher 'Created' -Action $action $changed = Register-ObjectEvent $watcher 'Changed' -Action $action $deleted = Register-ObjectEvent $watcher 'Deleted' -Action $action while ($true) { Start-Sleep -Seconds 1 }