Prerequisites
Steps to reproduce
$from="$env:systemdrive:\test1\*"
$to="$env:systemdrive:\test2"
remove-item $to -force -recurse
Copy-Item -path $from $to -Recurse -Force
Expected behavior
copy-item copies successfully without error message.
Actual behavior
Perceived non-deterministic error:
Copy-Item: Container cannot be copied onto existing leaf item.
*RFC*
https://github.com/PowerShell/PowerShell-RFC/blob/master/RFCNNNN-New-RFC-Template.md
Title: Copy-Item should not fail with error "Container cannot be copied onto existing leaf item"
Motivation:
As a [<<user_profile>>] -> (https://github.com/penalvch),
I can <<functionality>> -> What?!,
so that <<benefit>> -> Powershell doesn't setup people to fail on normal expectations of how copying a file or using a basic cmdlet like copy-item works (e.g. interoperability and expectations when context switching from different languages and computing norms).
User Experience: See above and below.
Specification: See above and below.
Alternate Proposals and Considerations: None.
Error details
get-error
Type : System.Management.Automation.ActionPreferenceStopException
ErrorRecord :
Exception :
Type : System.Management.Automation.PSArgumentException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Container cannot be copied onto existing leaf item.
HResult : -2146233087
CategoryInfo : InvalidArgument: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : Argument
Message : Container cannot be copied onto existing leaf item.
ParamName : path
HResult : -2147024809
TargetObject : C:\test2\.test
CategoryInfo : InvalidArgument: (C:\.test:String) [Copy-Item],
PSArgumentException
FullyQualifiedErrorId : CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand
InvocationInfo :
MyCommand : Copy-Item
ScriptLineNumber : 1
OffsetInLine : 38
HistoryId : 19
Line : remove-item $to -force -recurse ; Copy-Item -path $from $to -Recurse -Force
Statement : Copy-Item -path $from $to -Recurse -Force
PositionMessage : At line:1 char:38
+ … -recurse ; Copy-Item -path $from $to -Recurse -Force …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Copy-Item
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
0
1
TargetSite :
Name : Invoke
DeclaringType : [System.Management.Automation.Runspaces.PipelineBase]
MemberType : Method
Module : System.Management.Automation.dll
Message : The running command stopped because the preference variable "ErrorActionPreference" or common parameter is
set to Stop: Container cannot be copied onto existing leaf item.
Data :
System.Management.Automation.Interpreter.InterpretedFrameInfo, System.Management.Automation, Version=7.6.0.500,
Culture=neutral, PublicKeyToken=31bf3856ad364e35 : <ScriptBlock>
Source : System.Management.Automation
HResult : -2146233087
StackTrace :
at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline, Exception& exceptionThrown,
ExecutionOptions options)
Environment data
$psversiontable
Name Value
---- -----
PSVersion 7.6.1
PSEdition Core
GitCommitId 7.6.1
OS Microsoft Windows 10.0.26200
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.4
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals

Prerequisites
Steps to reproduce
Expected behavior
copy-item copies successfully without error message.Actual behavior
Error details
Environment data
Visuals