2

I want my PowerShell script to take a backup of $destination directory, in $backup directory, but the newly created backup directory should be named $destination-{yyyy-dd-mm} where 'yyyy-dd-mm' is today's date.

I have written my 1st ever PowerShell script which accomplishes the job with one caeat. I have noticed that it creates a folder if it does not exist already and copies all the files into this folder the 1st time it is executed, but on subsequent runs it skips all files, if there is no change. I want to forcefully overwrite the destination files regardless of their last change date.

"START"

$source = "C:\Sandbox\Source"
$destination = "C:\Sandbox\Target"
$backup = "C:\Backups\"

"Taking backup of '$destination' in '$backup'"

$currentDate = Get-Date
$currentDate = $currentDate.ToString("yyyy-MM-dd")
$something = $backup + "Staging-" + $currentDate
robocopy $targetPath $something /E /V /MT /Z

"END"
1
  • 1
    If you just want to copy, you could use the copy command. robocopy is made for synching.
    – harrymc
    Commented Aug 17, 2022 at 14:04

2 Answers 2

2

I want to forcefully overwrite the destination files regardless of their last change date.

Use the /IS switch - Include Same, overwrite files even if they are already the same where Same is a file class:

enter image description here

Source: Robocopy "Robust File Copy" - Windows CMD - SS64.com

1
  • 1
    I tried robocopy sourceDIR destDIR myFile.iso /IS but that does not rewrite the destination file.
    – SebMa
    Commented Apr 10 at 17:09
0

The robocopy argument you want, is " /IS".

$Arguments = @( '/IS', '/R:0', '/W:0', '/MT', '/E', '/A-:SH' )

$RoboCopy = 'C:\Windows\System32\Robocopy.exe'

$RoboCopyArgs = @($Source, $Destination, $Name) + $Arguments

& $RoboCopy @RoboCopyArgs

I have this robocopy powershell script for you. Hope you'll find it usefull.

https://www.tapuz.co.il/threads/%D7%94%D7%A2%D7%AA%D7%A7-%D7%A7%D7%91%D7%A6%D7%99%D7%9D-%D7%95%D7%95%D7%93%D7%90-%D7%99%D7%95%D7%A9%D7%A8%D7%AA-%D7%94%D7%94%D7%A2%D7%AA%D7%A7.15635089/post-145607055

1
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Toto
    Commented Dec 13, 2023 at 10:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .