Monday, June 7, 2010

PowerShell to monitor for file system changes

Quick snippet of PowerShell 2.0 code to register for file system events using the .Net framework:

#REQUIRES -version 2.0
##file system watcher
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "c:\"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$watcher
Register-ObjectEvent $watcher "Changed" -Action { Write-Host "Changed file $($eventArgs.Name)" }
Register-ObjectEvent $watcher "Created" -Action { Write-Host "Created file $($eventArgs.Name)" }
Register-ObjectEvent $watcher "Deleted" -Action { Write-Host "Deleted file $($eventArgs.Name)" }
Register-ObjectEvent $watcher "Renamed" -Action { Write-Host "Renamed file $($eventArgs.Name)" }

No comments: