Productivity Tools

Tools written by an IT business manager, a passionate developer.

Copy Item Directory Repeatable

In PowerShell Copy-Item bahaves differently when target folder exists and different when not. Module provides functionality which is idempotent.

When you are using Copy-Item the destination doesn’t exist it assumes you’re trying to copy/rename the source to be the destination, whereas if the destination exists it assumes you’re trying to copy underneath it. This results sometimes in copying directory to requested destination and sometimes to folder inside it.

Example below visualize the situation. Initial state:

After first Copy-Item invocation.

Copy-Item D:\Trash\Test\ D:\Trash\TestDest\Test -Recurse -Force

TestElement directory is copied under Test directory.

And after second Copy-Item invocation whole Test directory is copied to target Test.

Copy-Item D:\Trash\Test\ D:\Trash\TestDest\Test -Recurse -Force

Following module helps when you would like to copy content of one folder to other and always have the same result every time.

Copy-ItemDirectoryRepeatable D:\Trash\Test\ D:\Trash\TestDest\Test -Recurse -Force
Close Bitnami banner