Skip to content

Copy-Item -Include / -Exclude filters with -Recurse are unexpectedly applied to an input directory path itself #8459

Closed

Description

Related:

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)

Activity

  1. added
    Issue-Discussion
    on Dec 14, 2018
  2. kvprasoon commented on Dec 15, 2018

    @kvprasoon
    Contributor

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

  3. mklement0 commented on Dec 15, 2018

    @mklement0
    ContributorAuthor

    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?

  4. kvprasoon commented on Dec 16, 2018

    @kvprasoon
    Contributor

    I didn't notice the term directory , I tried with file and worked

    ♥->copy -Recurse -Path .\t.log -Include *.* -Verbose -Force -Destination C:\Temp\ -WhatIf 
    
    What if: Performing the operation "Copy File" on target "Item: C:\Windows\Temp\testcopy\t.log Destination: C:\Temp\t.log".
    

    and doesn't work for directory as you mentioned. And notice that the exclusion issue applies for both directory and path.

    ♥->copy -Recurse -Path .\t.log -Exclude t* -Verbose -Force -Destination C:\Temp\ -WhatIf
    
    ♥->
    
  5. mklement0 commented on Dec 16, 2018

    @mklement0
    ContributorAuthor

    It's actually not about file vs. directory, it's that *.* only matches names with an extension, so *.* doesn't match Desktop, but matches t.log.

    I've updated the original post to make it clear that *.* was chosen not to match input path Desktop, so as to demonstrate the problem.

  6. kvprasoon commented on Dec 16, 2018

    @kvprasoon
    Contributor

    Got it, Thanks ! *.* matches with directory with . in its name too.

  7. added
    Area-FileSystem-Provider
    WG-Engine-Providers
    and removed
    Issue-Discussion
    on Nov 29, 2021
  8. added
    Resolution-No Activity
    on Nov 16, 2023
  9. AlexGadd commented on May 13, 2026

    @AlexGadd

    This is still an issue, able to replicate on windows 11 with powershell 7.6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FileSystem-ProviderResolution-No ActivityWG-CmdletsWG-Engine-Providers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions