Skip to content

Instantly share code, notes, and snippets.

@PProvost
Created May 9, 2012 18:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PProvost/2647926 to your computer and use it in GitHub Desktop.
Save PProvost/2647926 to your computer and use it in GitHub Desktop.
Basic file watching in powershell
param(
[string] $rootFolder = ".",
[string] $filter = "*.*"
)
$path = resolve-path $rootFolder -errorAction Stop
write-host "Monitoring $path for changes"
$fsw = new-object System.IO.FileSystemWatcher $path, $filter
$fsw.IncludeSubdirectories = $true
$fsw.NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
$action = {
$name = $event.SourceEventArgs.Name
$changeType = $event.SourceEventArgs.ChangeType
$timeStamp = $event.TimeGenerated
write-host "The file '$name' was $changeType at $timeStamp" -fore green
}
register-objectEvent $fsw Created -SourceIdentifier FileCreated -Action $action
register-objectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action $action
register-objectEvent $fsw Changed -SourceIdentifier FileChanged -Action $action
# TODO: To disable this, call the following:
# unregister-event FileCreated
# unregister-event FileChanged
# unregister-event FileDeleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment