• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7525
  • Last Modified:

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?
  • 2
  • 2
1 Solution
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

hchabriaAuthor Commented:
Thank you so much. It's working perfectly for me.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Managed Security Services Webinar - March 15

Selecting the right managed security services platform to grow your business can be a huge undertaking. Join WatchGuard and Frost & Sullivan in an upcoming webinar as we dive into the key elements of selecting a vendor platform and partnership to fuel a successful MSSP business.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now