Search notes:

PowerShell cmdLet Copy-Item

copy-item copies an item (typically files or directories, but also registry keys) from one location to another.

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).

Recursively create backup files

The following simple pipeline uses get-childItem -recurse *.ps1 to recursively select all PowerShell scripts and then uses forEach-object to invoke copy-item to create a backup file (with the extension .bak) for each of these files.
get-childItem -recurse *.ps1 |
  foreach-object {
     copy-item  $_  $( $_.fullName -replace 'ps1$', 'ps1.bak' )
  }
Github repository about-powershell, path: /cmdlets/item/copy/create-backups.ps1

Aliases

copy and cp are aliases for copy-item. These names are apparently inspired by the cmd.exe command copy and the Shell command cp.

See also

The command parameter -credential.
Powershell command noun: item

Index