The following code is part of my script to filter txt files > 0 KB and send them by e-mail as attachments.
$file_path = "\\path\to\my\files"
$file = Get-ChildItem -Path $file_path -Filter "*.txt" -Recurse | where-object {$_.Length -gt 0} | sort-object LastWriteTime -Descending
I 've got the following files inside my folder $file_path
: test.txt | dummy1.txt | order.txt
The result of $file
is an array
and I attache each single file to an e-mail and send it by using Send-MailMessage -To $_to -Subject $_subject -Encoding ([System.Text.Encoding]::UTF8) -Body $_body -BodyAsHtml -SmtpServer $_server -From $_from -Attachments $_attachments
. This leads to an error, that the file could not be found.
Send-MailMessage : The "C:\WINDOWS\system32\test.txt" could not be found.
I don't understand, why the error comes? I mean, I defined the correct path in $file_path = "\\path\to\my\files"
but the error messages shows a way different path C:\WINDOWS\system32\
.
If I use -Include
instead of -Filter
it works and my e-mail with the attached files will be send correctly without any issues. Why does -Filter
leads to an error?