PowerShell Send RoboCopy summary result as email

Hi,

I was able to successfully write a RoboCopy commands to backup few directories which is located in few different devices and locations. I am planning to run this script via task scheduler at required timing.

Depends on the file sizes it took around 15mins to 5hours to complete the backup in the end I get the summary result which is very useful like below.

Robocopy_summary

Now what I am trying to added to this script is a way to only email the summary result but I am struggling to find a way and I will be appreciated if the result get as same as shown in the below image.

To email the summary result of your RoboCopy script, you can use PowerShell. Here’s a simple way to do it:

  1. Run RoboCopy and save the log:

    robocopy "source_directory" "destination_directory" /MIR /LOG:"C:\path\to\logfile.log"
    
  2. Send the log via email:

    $LogFile = "C:\path\to\logfile.log"
    $EmailFrom = "your_email@example.com"
    $EmailTo = "recipient_email@example.com"
    $EmailSubject = "RoboCopy Summary"
    $SMTPServer = "smtp.yourserver.com"
    $SMTPPort = 25
    
    $LogFileContents = Get-Content $LogFile
    $EmailBody = $LogFileContents -join "`n"
    
    Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $EmailBody -SmtpServer $SMTPServer -Port $SMTPPort
    
  3. Schedule the script:

    • Save the above script as a .ps1 file.
    • Use Task Scheduler to run the script at your desired times.

This should help you automate the process of emailing the RoboCopy summary result. If you need further assistance, feel free to ask!