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
}

  

posted @ 2023-05-03 16:04  Ender.Lu  阅读(39)  评论(0编辑  收藏  举报