Help with 'LastWriteTime' - Folder Checking script

Hi Experts,

Im using the following script on our sFTP server to notify team members when a new file is uploaded.

$AWS_ACCESS_KEY = "XXXXXXXXX"
$AWS_SECRET_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXX"
$SECURE_KEY = $(ConvertTo-SecureString -AsPlainText -String $AWS_SECRET_KEY -Force)
$creds = $(New-Object System.Management.Automation.PSCredential ($AWS_ACCESS_KEY, $SECURE_KEY))

$smtp = "email-smtp.eu-west-1.amazonaws.com"
$from = "SFTP@here.com"
$to = "upload@here.com"
$bcc = "them@here.com"
$subject = "SFTP Alert!"

$Path = "D:\DATA\YourData\"  
$excluded = @("*.log")

$File = Get-ChildItem $Path -Recurse -exclude $excluded | Where { $_.LastWriteTime -ge [datetime]::Now.AddMinutes(-10) }
If ($File)
{ $SMTPBody = "Hello Team <br /> The following data files have recently been uploaded: <br />"
$File | ForEach { $SMTPBody += "$($_.FullName)<br />" }
Send-MailMessage -From $from -To $to -Bcc $bcc -Subject $subject -Body $SMTPBody -BodyAsHtml -SmtpServer $smtp -Credential $creds -UseSsl -Port 587 

Its working but a bit too well. Quite often we will get notification even when there is not file copied. A few times lately, a notification has been sent when the folder timestamp has modified (no file transferred) - also I cant see what is causing the folder to get a modified timestamp :face_with_raised_eyebrow:

Is there a way to only monitor for files coming into the folder?

Many thanks
Fiorano

Tim,
Welcome to the forum. :wave:t4:

You can use the parameter -File to limit the output of Get-ChildItem to only files - no directories.