PowerShell Backup Script

Copy all your valuable Files to an external Drive, to a Network Folder or in the Cloud. Only configure your Folders you want to save, define your Destination, the Logging level and how many of your old backups you want to stay in your Destination.

BackupScript.zip
 
 
 
 
 
4.5 Star
(22)
20,439 times
Add to favorites
3/23/2019
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Sending Email of the Log file
    2 Posts | Last post May 28, 2018
    • Hi, Great Code Michael - It really helped me.
      
      I manage to set this up. If the Powershell Code is running in the backround through Task Scheduler. I am not aware as to what files were copied across to the External HDD.
      
      I built in an email send of the log file.
      
      Beginning of the code insert:
      
      #E-mail settings
      $SendEmail = $True                    # = $true if you want to enable send report to e-mail (SMTP send)
      $EmailTo   = 'test@domain.com'              #user@domain.something (for multiple users use "User01 <user01@example.com>" ,"User02 <user02@example.com>" )
      $EmailFrom = 'from@domain.com'   #matthew@domain 
      $EmailSMTP = 'smtp.domain.com' #smtp server adress, DNS hostname.
      
      at the end of the code.
      
      Logging "INFO" "Backupduration $Minutes Minutes and $Seconds Seconds"
          Logging "INFO" "----------------------"
          Logging "INFO" "----------------------" 
      }
      
      
      # Send e-mail with reports as attachments
      if ($SendEmail -eq $true) {
       
      $EmailSubject = "Backup Email $(get-date -format MM.yyyy)"
      $EmailBody = "Backup Script $(get-date -format MM.yyyy) (last Month).`nYours sincerely `Matthew - SYSTEM ADMINISTRATOR"
       
      Write-Host "Sending e-mail to $EmailTo from $EmailFrom (SMTPServer = $EmailSMTP) " -ForegroundColor Yellow
      
      
      ### the attachment is $log 
      Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -SmtpServer $EmailSMTP -attachment $Log 
       
      }
      
    • thanks, will be added to the next release
  • Error on $FileCount
    2 Posts | Last post May 28, 2018
    • Script does create the directories and copies the correct files but the file count isn't working.
      I get this error
      
      Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
      At line:127 char:9
      +         $FilesCount += Get-ChildItem $Backup -Recurse | Where-Object  ...
      +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
          + FullyQualifiedErrorId : MethodNotFound
      
      This is the code i have.
      
          foreach ($Backup in $BackupDirs) {
              $colItems = (Get-ChildItem $Backup -recurse | Where-Object {$_.mode -notmatch "h"} | Measure-Object -property length -sum) 
              $Items=0
              $FilesCount += Get-ChildItem $Backup -Recurse | Where-Object {$_.mode -notmatch "h"}  
              Copy-Item -Path $Backup -Destination $Backupdir -Force -ErrorAction SilentlyContinue
              $SumMB+=$colItems.Sum.ToString()
              $SumItems+=$colItems.Count
    • Hi,
      please make sure you run the Script with at least Powershell v3
  • Join paths to user source
    2 Posts | Last post May 28, 2018
    • Hello ! 
      
      First of all : Thank you so much, the script is awesome !
      
      I've made some changes in order to avoid any static source and destination for the backup : 
      
      $BackupDirs=Read-Host ’Source’              // exemple : C:\Users\Michael\
      $Destination=Read-Host – ‘Destination’      // F:\Backup 
      
      I would love to perform a backup based on the user typped as a Source and to focus on specific folders. How could I add the " C:\Users\Michael\ " to "\Documents\", "\Images\", "\Desktop\", "\Downloads\", "\Music\" in order to only copy these specific folders instead of ALL the datas of my user ? 
      
      So the user have only to type : C:\Users\Michael\
      And the backup would copy : C:\Users\Michael\Documents, C:\Users\Michael\Desktop, C:\Users\Michael\Images, C:\Users\Michael\Downloads, C:\Users\Michael\Music
      
      I tried "join-path" without success :(
      
      
      I would be really grateful if you could help me. 
      
      Thanks in advance,
      
      Andrei 
    • Hi, just enter each Path to the Variable:
      $BackupDirs="C:\Users\Micha\OneDrive", "C:\Program Files (x86)\OpenVPN" #What Folders you want to backup
  • Weird Error
    2 Posts | Last post January 15, 2018
    • Changed and added file locations and i get this error.
      + ... otmatch "h"} | Measure-Object -property length -sum)
      +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumen
         tException
          + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerSh
         ell.Commands.MeasureObjectCommand
      You cannot call a method on a null-valued expression.
      At C:\Users\Shauns Laptop.SHAUNS-LAPTOP\Documents\Power
      Shell\BackupScript.ps1:129 char:9
      +         $SumMB+=$colItems.Sum.ToString()
      +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
          + FullyQualifiedErrorId : InvokeMethodOnNull
      
      Yet it still runs and backs everything up??
      
      Any ideas would be great
      
      
    • Hi, have you solved this issue.
      As I am getting the same error.
  • Grouping by backupdirs
    1 Posts | Last post July 25, 2017
    • Michael, I use Your script daily. Like it. However, I would like to create a summary at the end of log file by backupdirs,  ie. backupdirs name, count of all files in particular backupdirs, count of successfully copied files of particular backupdirs and count of unsuccessfully copied files of particular backupdirs. Could you just point me to the right direction how to implement this task?!
      
      Thanks, Branko
  • Not backing up all files
    1 Posts | Last post December 15, 2016
    • The script appears to run just fine, the file count is correct, and logging indicates that all files were copied.  However the backup folder has far fewer files in it that the original folder, and the number indicated by the script/log.  
  • SubDir
    2 Posts | Last post November 04, 2016
    • New to script and powershell so bear with me.
      I noticed that the script has two source dirs listed...
      
      $BackupDirs="C:\Users\Michael\AppData\Roaming\TaskUnifier",
                  "C:\Users\Michael", ...
      
      I would assume that if I list a directory then it's subdirs are also copied? But then why is the AppData subdir listed above? Also, is there a way to exclude dir's like robocopy?
      Thanks 
    • Hi,
      you are absolut correct, in this case the first directory is not necessary
  • Automatically close script
    2 Posts | Last post October 13, 2016
    • Hello Michael,
      
      First, this is a wonderful script.  I would like to implement this as scheduled task, though, for some computers.  With that in mind, how can I set the script to close automatically when done?
      
      Thanks,
      Michael
    • hi,
      
      just remove the last two lines.
  • Hi Dear,
    2 Posts | Last post January 04, 2016
    • First of all thanks for the Scritp, it's very usefull. I'll like to have the possibility to copy ACL's as well and Log Path Files +256 Caracters and save in Log file the Full Path for each file in order to identify the source if some error apear in any file copy.
      
      Thanks a lot for the help.
    • Hi, thanks for your question, will add the ACL feature to the next version.
      256 characters is a limit of Powershelgl, so not sure how to deal with it, will give it a try.
      please rate
  • Files not backed up.
    3 Posts | Last post November 23, 2015
    • Please see excerpt of log file below. All files are being skipped when running your script. I have tried this on various servers and OS versions, and the problem persists. Any ideas as to where it is going wrong?
      
      
      23.11.2015-15:16:19 - INFO: ----------------------
      23.11.2015-15:16:19 - INFO: Start the Script
      23.11.2015-15:16:19 - INFO: Check if there are more than 7 Directories in the Backupdir
      23.11.2015-15:16:19 - INFO: Create Backupdir g:\Backups\Backup-2015-11-23-4264\
      23.11.2015-15:16:19 - INFO: Move Log file to g:\Backups\Backup-2015-11-23-4264\
      23.11.2015-15:16:19 - INFO: Continue with Log File at g:\Backups\Backup-2015-11-23-4264\
      23.11.2015-15:16:19 - INFO: Check if BackupDir and Destination exists
      23.11.2015-15:16:19 - INFO: Started the Backup
      23.11.2015-15:16:19 - INFO: Count all files and create the Top Level Directories
      23.11.2015-15:16:19 - INFO: There are 48 Files with  1.41 MB of Files to copy
      23.11.2015-15:16:19 - ERROR: antenna returned an error and was not copied
      23.11.2015-15:16:19 - ERROR: images returned an error and was not copied
      23.11.2015-15:16:19 - ERROR: about.htm returned an error and was not copied
      23.11.2015-15:16:19 - ERROR: antenna.css returned an error and was not copied
      23.11.2015-15:16:19 - ERROR: contact.htm returned an error and was not copied
    • Hi, maybe you have no access to source or destination, start Script as admin.
    • I start PowerShell as Administrator, but it makes no difference. 
31 - 40 of 41 Items