Skip to content

Instantly share code, notes, and snippets.

@bymyslf
Last active January 2, 2017 09:09
Show Gist options
  • Save bymyslf/4c956ff4b7bd4f4f60dd4695d7be63e9 to your computer and use it in GitHub Desktop.
Save bymyslf/4c956ff4b7bd4f4f60dd4695d7be63e9 to your computer and use it in GitHub Desktop.
PowerShell module to register a FileSystemWatcher to copy files from source folder to destination folder
function Register-Watcher-ToCopy {
param (
[string]$sourceFolder,
[string]$destFolder,
[string]$id
)
$filter = "*.*"
$watcher = New-Object System.IO.FileSystemWatcher $sourceFolder, $filter -Property @{
IncludeSubdirectories = $true
EnableRaisingEvents = $true
}
$messageData = New-Object psobject -Property @{
SourceFolder = $sourceFolder
DestFolder = $destFolder
}
$action = {
ROBOCOPY $Event.MessageData.SourceFolder $Event.MessageData.DestFolder /E /PURGE
}
Register-ObjectEvent -InputObject $watcher -EventName "Changed" -SourceIdentifier $id -MessageData $messageData -Action $action
}
Export-ModuleMember Register-Watcher-ToCopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment