Recently, I came across a scenario where there is a list which contains visitors' information and the admin team wants to get an email of Visitors for the following day.

This can be easily achieved using Timer Job or CSOM (using scheduler). I tried to achieve this using Powershell and Task Scheduler.

Here is my list.


Columns NamesData Type
Date of Visit Date and Time
Host People
Name fo visitor Single of Text
Company Single of Text
License Plate Number Single of Text
Created a View and applied Today+1 filter on the "Date of Visit" column.
Here is the PowerShell script to fetch the list data
  1. Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  2. $MyWeb = Get-SPWeb "http://WebApp/Web/"
  3. $MyList = $MyWeb.Lists["ListName"]
  4. $spQuery = New-Object Microsoft.SharePoint.SPQuery
  5. $camlQuery = '<Where><Eq><FieldRef Name="Date_x0020_of_x0020_visit"/><Value Type="DateTime"><Today OffsetDays="1"/></Value></Eq></Where>'
  6. $spQuery.Query = $camlQuery
  7. $spListItems = $MyList.GetItems($spQuery)
  8. Write-Host "Count: " $spListItems.Count
  9. if($spListItems.Count -gt 0)
  10. {
  11. $spListItems | foreach {
  12. $Name = $_["Host"].split('#')
  13. New-Object PSObject -Property @{
  14. "Host" = $Name[1]
  15. "Date of Visit"= $_["Date_x0020_of_x0020_visit"]
  16. "Name of Visitor" = $_["Name_x0020_of_x0020_vistor"]
  17. "Company" = $_["Company"]
  18. "Licence Plate Number" = $_["License_x0020_plate_x0020_number"]
  19. }
  20. } | Select-Object "Date of Visit", "Name of Visitor", "Company","Licence Plate Number", "Host" | Export-Csv -path 'D:\Reports\TodaysVisitors.csv' -NoTypeInformation
  21. }
Here is the script to send an email using PowerShell.
  1. #Define variables
  2. $fromaddress = "[email protected]"
  3. $toaddress = "[email protected]"
  4. $bccaddress = "[email protected]"
  5. $CCaddress = "[email protected]"
  6. $Subject = "Visitor's Data"
  7. $body ="Hello ,<br /> Please find attached reports of all Visitors who will be visiting tomorrow.<br />Do revert for any concerns. <br /> Note : This is system generated mail. <br /> In case of any further queries please contact [email protected] <br /><br /> Kind Regards, <br />SharePoint"
  8. $smtpserver = "SMPTServer"
  9. #Implementation Code
  10. $message = new-object System.Net.Mail.MailMessage
  11. $message.From = $fromaddress
  12. $message.To.Add($toaddress)
  13. $message.CC.Add($CCaddress)
  14. $message.Bcc.Add($bccaddress)
  15. $message.IsBodyHtml = $True
  16. $message.Subject = $Subject
  17. #your file location
  18. $files=Get-ChildItem “D:\Reports"
  19. Foreach($file in $files)
  20. {
  21. #Write-Host “Attaching File :- ” $file
  22. $attachment = New-Object System.Net.Mail.Attachment –ArgumentList D:\Reports\$file
  23. $message.Attachments.Add($attachment)
  24. }
  25. #$attach = new-object Net.Mail.Attachment($attachment)
  26. #$message.Attachments.Add($attach)
  27. $message.body = $body
  28. $smtp = new-object Net.Mail.SmtpClient($smtpserver)
  29. $smtp.Send($message)
  30. $attachment.Dispose();
  31. $message.Dispose();
The above PowerShell can be configured to Task Scheduler and here are the links for the same.
Schedule PowerShell Scripts Powershell Script with Arguments as a Scheduled Task
Here is the output in CSV file.

csv