Forum Discussion
Ganesh12
Oct 03, 2019Copper Contributor
"FileSystemWatcher "not able to monitor new files coming in sub directory for network share drive
Hi,
I was testing the monitor files using New-Object System.IO.FileSystemWatcher
Even I enabled the property the below property,its not working
$watcher.IncludeSubdirectories = $true
I am testing on Windows server 12 R2 on network share drive
1 Reply
- Kevin_MorganIron Contributor
Can you try the below script ?
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher $FileSystemWatcher.Path = "C:\RootFolder\TestFolder" $FileSystemWatcher.IncludeSubdirectories = $true $FileSystemWatcher.Filter = "*" $FileSystemWatcher.NotifyFilter = 383 #Register watcher to watch created changes alone. Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action { $Object = "{0} was {1} at {2}" -f $Event.SourceEventArgs.FullPath, $Event.SourceEventArgs.ChangeType, $Event.TimeGenerated $WriteHostParams = @{ ForegroundColor = 'Green' BackgroundColor = 'Black' Object = $Object } Write-Host @WriteHostParams }
Other supported EventName(s)
Changed,Renamed,Deleted and ErrorRefer this post for more info : https://mcpmag.com/Articles/2015/09/24/Changes-to-a-Folder-Using-PowerShell.aspx?Page=1