Category filter
Script to backup files and folders on Windows devices
Windows 10 has a plethora of ways to back up and restore your data – File History, System Image Recovery, System Restore, just to name a few. However, if you’re someone who wants to doubly secure their data backups just to be on the safe side, you can also achieve the same by executing a custom script that’ll backup your data wherever you want. You’ll always have your data copied in a location of your choice.
Powershell script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Param($src,$dest) If (Test-Path –Path $dest){ Write-Host “The target folder exists. All the older files in this folder will be deleted.” Remove-Item $folder –Recurse –Force } else{ Write-Host “The target folder does not exist. A new folder will be created to store the backup files and folders.” } Copy-Item –path $src –Destination $dest –recurse |
- The admin can pass the source and target directory parameters.
- $src & $dest – Source and destination files to be entered as Arguments under Manage > Devices > (Chosen Device) > Actions > Execute Custom Script.
- The Test-Path cmdlet determines whether all elements of the path exist. It returns $True if all elements exist and $False if any are missing. It can also specify whether the path syntax is valid and leads to a container, terminal or a leaf element.
- Write-Host – Write-Host is a wrapper for Write-Information. This allows you to use Write-Host to emit output to the information stream. It also enables the capture or suppression of data written using Write-Host while preserving backward compatibility.
- Remove-Item – deletes the specified items
- Copy-Item – copies the specified items.
- In the event that an older version of the file to be backed up exists, it will subsequently be deleted and replaced with the newer updated version. If not, a new file will be created in the target location to store the backed-up file.
Need more help?