Issue 1: How to compare two directories?

Resolution: Take a snapshot of the original directory, wait a certain amount of time to take a second snapshot, and then compare the two. **Reference:** See the article, Finding New Files and Folders.

Issue 2: How to get the output of Compare-Object so that it can be emailed to the user?</p>

Resolution: Append the output to a text file. I used the ΓÇôPassThru parameter and >$report.txt to append the output to a text file.


Issue 3: How to send an email through powershell?

Resolution: Google search for ΓÇ£Send smtp email with powershellΓÇ¥ (see script for syntax to send an email)

Notes: As long as you are sending within the domain, you do not need to worry about credentials. In addition, it does not matter what email address you type into the ΓÇ£from ΓÇ£ field, as it is not checked to see if it is a valid email address or not.

Issue 4: How to setup the script so that it only emails the user when something has changed in the directory?

Resolution: Setup an IF statement. The IF statement in the script says that if the report.txt file is greater than 0kb, call the function ΓÇ£sendEmailΓÇ¥


Issue 5:  How to put the script in a loop and have it wait a certain amount of time before taking the snapshot again?

Resolution: use the start-sleep Cmdlet


The script is provided below:

## This script can monitor multiple directories
## Change the information below as needed</p>

## directory to monitor
## change as necessary

## Generated report of new files
## make sure the location of the report file is not in the same location as the directory you a monitoring

## Email portion, change as necessary
$EmailTo="Firstname Lastname <>"
$EmailSubject="Subject Goes Here"
$EmailBody="Body Goes Here"
$EmailFrom="Firstname Lastname <>"

## =========================================================
## ======== PLEASE DO NOT EDIT SCRIPT BELOW ================
## =========================================================


while($val -eq 1){

$shot2 = Dir $dir

Compare-Object $shot1 $shot2 -PassThru >$report

function sendEmail{
send-mailmessage -from $EmailFrom -to $EmailTo -subject $EmailSubject -body $EmailBody -Attachments "$report" -priority High -dno onSuccess, onFailure -smtpServer $EmailSMTPServer

$File = Get-ChildItem $report
if ($File.Length -gt 0) {sendEmail}

$shot1 = Dir $dir

# this will pause the script for 60 seconds, change as necessary
Start-Sleep -s 60