File Backup with PowerShell

Backing up files is easy with PowerShell. We need the file copy command and task scheduler command. We can complete the process with Robocopy and a new task.

A simple code example for Robocopy

robocopy.exe <SourceDestination> <TargetDestination> /mir /mt:96 /r:3 /w:3 /np /ts /bytes /xd > c:\log.txt
information for robocopy parameters
/mir tells robocopy to use mirror mode
/mt:96 maximum of allowed threads increasing or decreasing this results in different end-times
/r:3 maximum set retry count
/w:3 maximum set wait time before a retry is executed
/np shows no progress
/ts include the timestamp of the source file
/bytes display progress in bytes for easy calculation
/xd $exclude exclude files that set in the "$exclude" variable
> c:\log.text export log.txt


We use the New-ScheduledTaskAction command to create a task. 

For example 

New-ScheduledTaskAction -Execute "PowerShell.exe"
So we use the New-ScheduledTaskTrigger command to scheduled a task. 

New-ScheduledTaskTrigger -At 10:00am –Daily

Let's write our ps script

$Trigger= New-ScheduledTaskTrigger -At 10:00am –Daily $User= "NT AUTHORITY\SYSTEM" $Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "robocopy.exe <SourceDestination> <TargetDestination> /mir /mt:96 /r:3 /w:3 /np /ts /bytes /xd > c:\log.txt" Register-ScheduledTask -TaskName "test" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force
Let's test 

Step 1: creating two folders 

Soruce destination C:\a
Target destination C:\b

Step 2: I'm uploading test files to the "a" folder.


Step 3: editing my code in source ande target destinations

$Trigger= New-ScheduledTaskTrigger -At 10:00am –Daily $User= "NT AUTHORITY\SYSTEM" $Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "robocopy.exe C:\a C:\b /mir /mt:96 /r:3 /w:3 /np /ts /bytes /xd > c:\log.txt" Register-ScheduledTask -TaskName "test" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Forc    

Test all PS script

PS C:\Users\Administrator\Desktop> $Trigger= New-ScheduledTaskTrigger -At 10:00am -Daily
PS C:\Users\Administrator\Desktop> $User= "NT AUTHORITY\SYSTEM"
PS C:\Users\Administrator\Desktop> $Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "robocopy.exe C:\a C:\b /mir /mt:96 /r:3 /w:3 /np /ts /bytes /xd > c:\log.txt"
PS C:\Users\Administrator\Desktop> Register-ScheduledTask -TaskName "test" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force
 
TaskPath                                       TaskName                          State
--------                                       --------                          -----
\                                              test                              Ready
 
 
PS C:\Users\Administrator\Desktop>
PS C:\Users\Administrator\Desktop>
PS C:\> dir
 
 
    Directory: C:\
 
 
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       28.06.2019     11:21                a
d-----       28.06.2019     12:14                b
d-----       15.09.2018     10:19                PerfLogs
d-r---       27.06.2019     15:42                Program Files
d-----       20.06.2019     09:24                Program Files (x86)
d-r---       12.06.2019     07:38                Users
d-----       21.06.2019     13:42                Windows
-a----       28.06.2019     12:14           2494 log.txt
 
 
PS C:\>
PS C:\> cd a
PS C:\a> dir
 
 
    Directory: C:\a
 
 
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       28.06.2019     11:21                New folder
d-----       28.06.2019     11:21                New folder (2)
d-----       28.06.2019     11:21                New folder (3)
d-----       28.06.2019     11:21                New folder (4)
-a----       28.06.2019     11:21              0 New Text Document (2).txt
-a----       28.06.2019     11:21              0 New Text Document (3).txt
-a----       28.06.2019     11:21              0 New Text Document.txt
 
 
PS C:\a> cd..
PS C:\> cd b
PS C:\b> dir
 
 
    Directory: C:\b
 
 
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       28.06.2019     11:21                New folder
d-----       28.06.2019     11:21                New folder (2)
d-----       28.06.2019     11:21                New folder (3)
d-----       28.06.2019     11:21                New folder (4)
-a----       28.06.2019     11:21              0 New Text Document (2).txt
-a----       28.06.2019     11:21              0 New Text Document (3).txt
-a----       28.06.2019     11:21              0 New Text Document.txt
 
 
PS C:\b> cd..
PS C:\>

Hopefully, it has been a useful solution.