Today I will explain on how to use Powershell to copy a complete folder including subfolders. In my previous post you can read about copying to the desktop folder regardless the physical storage location. In that post I promised to write this one.
I used to write scripts in plain old batch files and when I needed to copy files I nearly always used Robocopy for it. I still believe Robocopy is one of the best options in a Windows environment to copy files. This is true especially if the same copy action needs to be executed on a regular interval. Robocopy is also great if you want to make sure that two folders are exact copies of each other. However I also find myself writing PowerShell scripts more often. When I decide to write a PowerShell script and I need to copy something, I think using the native PowerShell method to copy files is preferable.
Say you want Powershell to copy the complete folder contents of \\Server\Share\Folder to C:\LocalCopy. You can do so using the following command:
Copy-Item "\\Server\Share\Folder\*" -Destination "C:\LocalCopy" -Recurse
Note that I ended with \* behind the source folder. If you omit this and just end with ..\Folder than you will copy the folder itself instead of it’s contents and you would find the folder in the destination to be C:\LocalCopy\Folder.
What makes PowerShell really stand out is that you can also use the option -WhatIf inside the command:
Copy-Item "C:\temp\SourceFolder" -Destination "C:\temp\DestinationFolder\" -Recurse -WhatIf What if: Performing the operation "Copy Directory" on target "Item: C:\temp\SourceFolder Destination: C:\temp\DestinationFolder\SourceFolder".
You can see that PowerShell will tell you what would happen if you were to execute the command (without actually executing the command). The difference with ending the source folder with \* is now clear before actually starting the copy action.
Copy-Item "C:\temp\SourceFolder\*" -Destination "C:\temp\DestinationFolder\" -Recurse -WhatIf What if: Performing the operation "Copy Directory" on target "Item: C:\temp\SourceFolder\SubFolder Destination: C:\temp\DestinationFolder\SubFolder". What if: Performing the operation "Copy File" on target "Item: C:\temp\SourceFolder\Doc1.docx Destination: C:\temp\DestinationFolder\Doc1.docx". What if: Performing the operation "Copy File" on target "Item: C:\temp\SourceFolder\Doc2.docx Destination: C:\temp\DestinationFolder\Doc2.docx". What if: Performing the operation "Copy File" on target "Item: C:\temp\SourceFolder\File1.txt Destination: C:\temp\DestinationFolder\File1.txt". What if: Performing the operation "Copy File" on target "Item: C:\temp\SourceFolder\File2.txt Destination: C:\temp\DestinationFolder\File2.txt".
Note that the -WhatIf option only tells you what it copies from the selected folder. It will not list the contents of any subfolders.
2 thoughts on “PowerShell complete folder copy”
great article, I prefer the GUI way which presents on t tools like GoodSync, Gs Richcopy 360 and Carbonite