PowerShell Script to attach file through email

I am running PowerShell v1.0 on Windows Server 2003. I run a daily script that stores the output in an Excel fiel in a particular folder (C:\Reports\). The format of the Excel file is "Citrix Morning Checklist_<Current Date>@<Current Time>.xls".

I want to schedule a PowerShell script that will pick up the most updated .xls file from the location (C:\Reports\) and send it as an attachment through mail.

Can you please help me out by providing the powershell script to do the same?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Prashant GirennavarCommented:
Check if below code works

$smtpServer = "<SMTP Server address goes here>" # SMTP Server address goes here Eg - Smtp.contoso.com
$msg = new-object Net.Mail.MailMessage # Add Message .Net class
$File = "C:\Reports\abc.xls" #File to be attched
$att = new-object Net.Mail.Attachment($File) #Attachement
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
[string]$body = "Please find the attachment"
$msg.From = "abc@contoso.com" #From address in the email
[String[]]$Recipient ='User1@Contoso.com','User2@contoso.com' #Recepeint who willl receive emais
$Recipient | % {$msg.to.add($_)}
$msg.subject = "Citrix Report"
$msg.body = $body

Open in new window


-Prashant Girennavar.
"Citrix Morning Checklist_<Current Date>@<Current Time>.xls
What is the date and time format in file name? can you give a sample name?

Or is it ok if we attach the last modified file from C:\Reports?
hchabriaAuthor Commented:
Hi PrashantGirennavar,

The file name is dynamic because of date and time stamp. I cannot mention a static file name as an attachment.

Hi Subsun,

The file name is "Citrix Morning Checklist_12-09-2013@06-30-16".

Yes, it is OK to send the last modified file.
Try this it will send your the last modified file... Change the mail addresses and smtp server as per your environment..
$File = (GCI C:\Reports | Sort LastWriteTime -Descending | Select -First 1).FullName
Send-MailMessage -From "From@domain.com" -To "To@domain.com" -Subject "Citrix Morning Checklis" -Attachments $File -SmtpServer smtp.domain.com -Body "Citrix Morning Checklist"

Open in new window

Or in ps v1.0
$attachment = (GCI C:\Reports | Sort LastWriteTime -Descending | Select -First 1).FullName

Function SendEmail
{ param($from,$to,$subject,$body,$smtphost,$attachment)
$body = $body
$att = new-object Net.Mail.Attachment($attachment)
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from,$to,$subject,$body
SendEmail -from "From@Domain.com" -to "To@domain.com" -subject "Citrix Morning Checklist" -body "Citrix Morning Checklist" -attachment $attachment -smtphost "smtp.domain.com"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hchabriaAuthor Commented:
Thank you so much. It's working perfectly for me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.