Note: The issue is closely related to #2934 and also to #7005
Said issue describes the surprising behavior of Copy-Item copying the _content_ of source directories to the target directory if that target directory didn't previously exist, rather than the source directories _as a whole_, which is what happens if the target _did_ exist.
In the case below, in addition to the inconsistency described, a bug surfaces when _more than 1_ directory is copied to a non-preexisting target directory.
If there happens to be _exactly 1_ directory among the source paths, that directory is effectively ignored (because, I think, its copy becomes the target directory _itself_).
$ErrorActionPreference = 'Stop'
trap { Pop-Location }
# Create and switch to a playgroud folder.
$tempDir = Join-Path ([io.path]::GetTempPath()) $PID
$null = New-Item -Force -ItemType Directory $tempDir
Push-Location $tempDir
# Create a 'source' directory with *2* subdirs, 'subd1', 'subd2'
# Note: Whether or not these subdirs. are empty is irrelevant.
$null = new-Item -force -ItemType Directory source
$null = new-Item -force -ItemType Directory source/subd1
$null = new-Item -force -ItemType Directory source/subd2
# Ensure that the 'dest' directory doesn't exist yet.
if (Test-Path ./dest) { Remove-Item -Force -Recurse dest }
# Copy the *content* of 'source' to 'dest'.
# Currently BREAKS.
# Note
# * The problem only surfaces if 'dest' is created *on demand*, i.e., didn't
# already exist.
# * If there were only *1* subdir., it would effectively be QUIETLY IGNORED.
# because Copy-Item would effectively copy that subdir. *as 'dest'*.
# * Whether or not you use -Recurse and/or -Force makes no difference
# here.
Copy-Item source/* dest -rec
# List what was copied to 'dest'
Get-ChildItem -Name -Recurse dest
Pop-Location
Remove-Item $tempDir -Recurse
subd1
subd2
Nothing gets copied to dest and the following error occurs:
Copy-Item : Container cannot be copied onto existing leaf item.
PowerShell Core v6.1.0-preview.2 on macOS 10.13.4
PowerShell Core v6.1.0-preview.2 on Ubuntu 16.04.4 LTS
PowerShell Core v6.1.0-preview.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Windows PowerShell v5.1.17134.48 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Totally agree, that the Copy-Item weirdnesses need to be resolved. I myself have tripped over those a couple of times when writing very simple CI deployment scripts...
The current behaviour when copying multiple items onto a nonexistent target is to copy each item onto the target item, e.g. copy a.txt,b.txt,c.txt d.txt copies a.txt onto d.txt, b.txt onto d.txt, etc. (IIRC the intent was to replicate the behaviour of cmd.exe which concatenates the three files into the destination but that obviously didn't happen since it replaces rather than concatenates). Now if the items being copied are directories you get an error because you can't copy one directory on top of another.
Most helpful comment
Totally agree, that the
Copy-Itemweirdnesses need to be resolved. I myself have tripped over those a couple of times when writing very simple CI deployment scripts...