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.

4.5 Star
21,429 times
Add to favorites
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question

  • Backup Delete Directory Incorrect
    4 Posts | Last post May 28, 2018
    • Thanks for the script - perfect start for me as new to Powershell. 
      However I noticed the tidy up of the backup folders is incorrect. It is deleting the latest folder ie not the new one created but the one before it, rather than the earliest folder which meets the retention criteria. The consequence is older folders are retained and the one immediately before the most recent is deleted. 
      I've changed the Delete-Backupdir command so to 
         Sort-Object -Property $_.LastWriteTime -Descending:$True
      ie Descending set to True not False. 
      This then works OK & delete the oldest folder. I presume could also change to Ascending:$False??
    • Mmmm. My correction now doesn't seem to be working either. The selection of the folder to delete seems to be rather random. Re-set to False and seems to be working OK. I'd suggest ignoring all my comments unless anyone knows why such strange effects. Could it be becasue I'm running the script within a minute or 2 of previous run?
    • I have correct this by Change Function Delete-Backupdir command to 
      $Folder=Get-ChildItem $Destination | where {$_.Attributes -eq "Directory"} | Sort CreationTime | Select -First 1 
    • Hi,
      this is fixed by next release
  • Upload to OneDrive/GoogleDrive
    3 Posts | Last post May 28, 2018
    • Hi 
      Firstly the script is good.
      Although i am unable to see any comments or option how do i upload to cloud (i.e. onedrive/google drive)
      Please guide me as i am new to powershell.
      Thanks a tonn in advance and really appreciate your reply.
    • hi,
      at the moment there is now integration with cloud Services, but you can map your destination to your mapped cloud Drive
    • Hi, that will be fixed by next release
  • ZIP Support
    2 Posts | Last post May 28, 2018
    • Hello, 
      Please add ZIP support of backups, mean save backup folder as ZIP 
      optionally add options to select  type of backup ZIP or Folder in #Variables filed.  
    • Hi,
      will be added with next release
  • 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) 
              $FilesCount += Get-ChildItem $Backup -Recurse | Where-Object {$_.mode -notmatch "h"}  
              Copy-Item -Path $Backup -Destination $Backupdir -Force -ErrorAction SilentlyContinue
    • 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,
    • 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
          + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerSh
      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...
                  "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?
    • Hi,
      you are absolut correct, in this case the first directory is not necessary
31 - 40 of 44 Items