Copy directories recursively
In order to copy a directory with its subdirectories and their files to another directory, the -recurse
parameter is required. Without this parameter, a directory will be created, but it will be empty.
There are slight nuances how a command to copy a directory must be entered: it depends on whether the destination directory already exists or if the command should create it.
Destination directory does not exist
If the destination directory (named dest
) does not exist, the following will create it:
copy-item -recurse src dest
dest
won't have a src
directory after copying it.
Destination directory already exists
If the destination directory already exists, the source directory can be copied into the destination directory with one of the following
commands:
copy-item -recurse src dest
copy-item -recurse src/* dest
The first command will create a subdirectory named src
below dest
while the second command does not create such a src
directory.
Thus, the result of the second command is the same as the result if the destination directory does not exist (see above).