You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Copy-Item -Include / -Exclude filters with -Recurse are unexpectedly applied to an input directory path itself #8459
While adding -Recurse to Get-ChildItem fixes the problem, the same is not true for Copy-Item, which continues to apply -Include and -Exclude filters to the input directory name too, which is especially problematic with -Include:
Workaround (which is the conceptually better solution to begin with): use -LiteralPath instead of the (positionally implied) -Path parameter; i.e., in the commands below, replace Desktop with -LiteralPath Desktop.
Steps to reproduce
Set-Location$HOME'---'# Note: Wildcard *.* does NOT match 'Desktop', because it has no extension.Copy-Item-Recurse Desktop /tmp -Include *.*-WhatIf
'---'Copy-Item-Recurse Desktop /tmp -Exclude desk*-WhatIf
Expected behavior
---
What if: Performing the operation "Copy Directory" on target "Item: C:\Users\jdoe\Desktop Destination: C:\tmp\Desktop"
---
What if: Performing the operation "Copy Directory" on target "Item: C:\Users\jdoe\Desktop Destination: C:\tmp\Desktop"
That is, both commands should indicate that a copy operation will take place (even though the details aren't mentioned).
Actual behavior
---
---
That is, both commands were unexpectedly quiet no-ops, because -Include filter *.* and -Exclude filter desk* were both applied to Desktop itself, eliminating the one and only input path.
Environment data
PowerShell Core v6.2.0-preview.2 on macOS 10.14.1
PowerShell Core v6.2.0-preview.2 on Ubuntu 18.04.1 LTS
PowerShell Core v6.2.0-preview.2 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.407)
Windows PowerShell v5.1.17134.407 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.407)
copy with -Include *.* in 6.2.0-Preview.3 works for me for this scenario. And isn't the -Exclude source path behaviour an expected one ? Problem occurs when there are items with similar matches and user need to exclude only those. Should be intelligent enough to exclude the source path...
copy with -Include . in 6.2.0-Preview.3 works for me for this scenario
Intriguing. I just tried on the following platforms with preview 3, and it does not work for me, meaning that there's no output; what is your exact invocation scenario?
PowerShell Core v6.2.0-preview.3 on macOS 10.14.2
PowerShell Core v6.2.0-preview.3 on Ubuntu 18.04.1 LTS
PowerShell Core v6.2.0-preview.3 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.471)
Windows PowerShell v5.1.17134.407 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.471)
Should be intelligent enough to exclude the source path...
Yes, if you provide a container as the source path, combined with -Recurse (arguably, even without it, but that's a separate issue), the wildcard should not be applied to the container itself, only to its descendants - that's how it (sensibly) works with Get-ChildItem.
And isn't the -Exclude source path behaviour an expected one ?
In light of the above: what about the current behavior is expected?
Activity
kvprasoon commentedon Dec 15, 2018
copy with
-Include *.*in 6.2.0-Preview.3 works for me for this scenario. And isn't the-Excludesource path behaviour an expected one ? Problem occurs when there are items with similar matches and user need to exclude only those. Should be intelligent enough to exclude the source path...mklement0 commentedon Dec 15, 2018
Intriguing. I just tried on the following platforms with preview 3, and it does not work for me, meaning that there's no output; what is your exact invocation scenario?
Yes, if you provide a container as the source path, combined with
-Recurse(arguably, even without it, but that's a separate issue), the wildcard should not be applied to the container itself, only to its descendants - that's how it (sensibly) works withGet-ChildItem.In light of the above: what about the current behavior is expected?
kvprasoon commentedon Dec 16, 2018
I didn't notice the term
directory, I tried with file and workedand doesn't work for directory as you mentioned. And notice that the exclusion issue applies for both directory and path.
mklement0 commentedon Dec 16, 2018
It's actually not about file vs. directory, it's that
*.*only matches names with an extension, so*.*doesn't matchDesktop, but matchest.log.I've updated the original post to make it clear that
*.*was chosen not to match input pathDesktop, so as to demonstrate the problem.kvprasoon commentedon Dec 16, 2018
Got it, Thanks !
*.*matches with directory with.in its name too.AlexGadd commentedon May 13, 2026
This is still an issue, able to replicate on windows 11 with powershell 7.6.1