Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Each of our jobs has a folder created by a batch file triggered by our accounting software at the time the job is entered. In that folder is a subfolder called "Sales Pictures". I would like to be notified by e-mail when files are created in any of the "Sales Pictures" folders under any job. Thankfully I don't need to go back and change every one of the 50000 or so sales pictures folders, only newly created folders. Is this possible in windows 2003 R2 using the command line with either no 3rd party software or free or open source software?


share|improve this question
up vote 2 down vote accepted

I haven't tested the syntax but off the top of my head these PowerShell lines should work :

$query = "Select * from __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'CIM_DataFile' AND TargetInstance.Drive='C:' AND TargetInstance.Path='\\mypath\\'"

Register-WmiEvent -Query $query -Action {send-mailmessage -to "" -from "" -subject "new file" -body " New file created " + ($event.SourceEventArgs.NewEvent.TargetInstance | Select -Expand FileName, Extension, Name)}

Where mypath is the name of the path and you'll need to put your own email addresses in. You may also have to add the -smtpserver parameter if the $PSEmailServer preference variable has not been set

This creates a temporary wmi event and consumer - meaning it works as long as the console is open. If you wanted a permanent provider you can create one with mofcomp or use a module like powerevents to make it a little easier to create the event. Permanent events will survive reboots etc.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.