Monitor file changes in Windows with PowerShell and pswatch

Sometimes I have wanted to monitor a directory for any file changes and receive alerts of those changes, for instance, when trying to find where an application is saving a configuration file. One solution I have come across is the small PowerShell module pswatch, which José F. Romaniello created.

How pswatch works

Pswatch is actually fairly simple in its design as it uses the .NET class System.IO.FileSystemWatcher. It returns the full path to any created, changed, deleted, or renamed file inside a directory. In addition, it can search within all subfolders of a directory.

A great feature of this module is that it continuously monitors directories, and since it writes the paths of files that change to the output, users can use a foreach loop in PowerShell and continuously perform logic on these objects. Obviously, there are numerous use cases for this.

Installing the module

The module unfortunately is not available in the PowerShell Gallery, but the creator’s GitHub page does provide an installation PowerShell script that will create the module on a local machine. We can use this via Invoke-Expression:

PS C:\Users\dan\Documents> iex ((new-object net.webclient).DownloadString(""))
Creating module directory
Downloading and installing
Use "Import-Module pswatch" and then "watch"
PS C:\Users\dan\Documents> Import-Module pswatch

Monitoring a folder and sending email alerts

One simple example of using the module is monitoring a folder for changes and then emailing a user when a change occurs. To use the pswatch module, we use the command watch and follow this with a path to the folder we want to monitor. Here is an example of code that would do just that:


Comments are closed.