skip to main content

Using PowerShell and robocopy to move files

This is an old PS v2 script I restored from the backup

Robocopy is my favorite way to copy or move files. I have incorporated its use in almost all of my PowerShell scripts.

In its simplest form, I use it to copy files from one directory to another locally.

$today = get-date
$today = $today.tostring("yyyy-MM-dd")
$src="E:\code\ps\robocopy\src"
$dst="E:\code\ps\robocopy\dst"
$logs="E:\code\ps\robocopy\logs"
$logfile = "$($logs)\$($today).log"
$file="*.txt"

$start_time = [datetime]::now
ac $logfile "Started moving old backups at: $($start_time)"

robocopy $src $dst $file /mov /e /zb /r:2 /w:5 /fp /np /v /log+:$logfile

$end_time = [datetime]::now
ac $logfile "Ended moving old backups at: $($end_time)"

$ts = new-timespan $start_time $end_time 
$ts = [string]::Format("`nTotal Time Elapsed: {0:d3} hours, {1:d3} minutes, {2:d3} seconds",$ts.hours,$ts.minutes,$ts.seconds)

ac $logfile "$($ts)"

I find these to be sane defaults for this simple use case.

  • /MOV :: MOVe files (delete from source after copying).
  • /E :: copy subdirectories, including Empty ones.
  • /ZB :: use restartable mode; if access denied use Backup mode.
  • /R:n :: number of Retries on failed copies: default 1 million.
  • /W:n :: Wait time between retries: default is 30 seconds.
  • /FP :: include Full Pathname of files in the output.
  • /NP :: No Progress - don't display % copied.
  • /V :: produce Verbose output, showing skipped files.
  • /LOG+:file :: output status to LOG file (append to existing log).