Subscribe Now

* You will receive the latest news and updates on the Canadian IT marketplace.

Trending News

Blog Post

How to: Enable Auto Notification of Specific File Changes via PowerShell
HOW TO

How to: Enable Auto Notification of Specific File Changes via PowerShell 

Setting up a file watcher seemed to address the issue and it was actually simple to script.

The following was created to watch the intended file for changes:

1
2
3
4
5
6
7
8
9
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = 'C:\temp\'
$watcher.Filter = 'test1.txt'
$watcher.EnableRaisingEvents = $true
  
$changed = Register-ObjectEvent
   $watcher 'Changed' -Action {
write-output "Changed: $($eventArgs.FullPath)"
}

Creating the file watcher took the utilization of the FileSystemWatcher object. This script specifically watches the entire directory and path for changes. An added filer is added to specify the required file.

Next, an ObjectEvent is registered to perform an action when the watcher detects a change event. This script highlights a simple output, however, it could be easily modified to enable sending of an email or performing some other task.

Removing the ObjectEvent is just as easy and can be performed using the following script:

1
Unregister-Event $changed.Id

Source: CANITPRO

Related posts