Posted in : Microsoft, Powershell, Windows Server Av Gus Johansson Översätt med Google ⟶

4 years ago

I have stumbled upon technical scenarios which required special maneuvers requiring skills I did not possess. After some reading I was able to conclude that Windows environments have several ways to copy files from a to b: good old ctrl +c and ctrl +v, xcopy, Copy-Item and robocopy. I was aware of the first one, but the other three and their capabilities was something I had to learn.
In this blog post I would like to highlight robocopy and its functions and usability. Microsoft Docs provides a very short and concise explanation of robocopy ”Copies file data”, which I aim to extend with a few more words with this post. I will limit the information to pros and cons, examples, and later contribute with my point of view on regarding the matter. The source and destination (below) can be any folders on any devices.
Robocopy (Robust File Copy)
• Ability to tolerate network interruptions and resume copy.
• Ability to set number of retries and wait time.
• Progress indicator. For an example, see image [1] below
• Ability to copy file data, attributes, preserve original timestamps and NTFS ACLs, owner and audit information (requires /COPYALL /COPY)
• Very good copy options
• Default value of retry is 1 000 000 and 30 seconds of wait time. If you attempt to transfer files which will fail robocopy will re-attempt for ~57 years [2].
• A bit complex for some scenarios due to a lot of options
The variables for the testing (PowerShell):

# Source folder
$source = "C:\Users\demo\Downloads"
# Destination folder
$destination = "C:\Users\demo\Downloads\Destination"

Official syntax from Microsoft Docs:
robocopy <Source> <Destination> [<File>[ …]] [<Options>]
To simply copy the content from within a directory to another, see below. By default, this is redundant with a million retries and thirty seconds of wait time in between the attempts. Remember that there are several ways to achieve the same results. I am showing you one of the many ways.

robocopy $source $destination

To copy a specific file from within the directory. If the file parameter is not included the default is to include all (*.\*). Specify this by using:

robocopy $source $destination "Citrix_Virtual_Desktop_Handbook_715-LTSR.pdf"

This is very easily inserted into a For each loop to distribute a folder or file to several hosts:

$source = "C:\Users\demo\Downloads"
$listofhosts = "demo-app1","demo-app2","demo-app3","demo-app4","demo-app5"
foreach ($host in $listofhosts) {
    Robocopy $source "\\$host\c$\Demo\"

Instead of making examples of all helpful options I thought I would make a list which summarizes them instead. You simply use the above PS scripts and then build upon it to match robocopy to your challenge. More options are available at Microsoft Docs.
Helpful options:

  • /log:<pathtologfile.txt> Creates a log file with all information including progress indication.
  • /eta Shows the estimated time of arrival for the files.
  • /max:<N>Specify the maximum/minimum size of the files to be included in the robocopy.
  • /min:<N> Specify the maximum/minimum size of the files to be included in the robocopy.
  • /mov Moves files and deletes from source and deletes after successful move.
  • /move Moves files and directories from source and deletes after successful move.
  • /rh:hhmm-hhmm Specifies a run time.
  • /s Copies subdirectories and excludes empty directories.
  • /e Copies subdirectories and includes empty directories.

Thank you for reading and please provide your mistakes, experience and knowledge of robocopy or the other cmdlets.

References and image:
[1] Image including the output of robocopy.
1. Progress indicator
2. Timestamps, timestamps are always good
3. Summary of progression
4. Speed of transfer/copy
5. Specifies file currently being transferred.

[2] Default value of retries are a million, with thirty seconds wait time is three million. Divided by sixty for hours, divided by twenty four for days, divided by three hundred sixty five for years.
1000000 x 30 = 30000000, / 60 = 500000, /24 = 20833, / 365 = 57,07

Tags :

Personlig rådgivning

Vi erbjuder personlig rådgivning med författaren för 1400 SEK per timme. Anmäl ditt intresse i här så återkommer vi så snart vi kan.

Add comment

Your comment will be revised by the site if needed.