Skip to content

Get-ChildItem - Can't Exclude Folders #19654

@aksarben

Description

@aksarben

Prerequisites

Steps to reproduce

I’m calling Get-ChildItem as shown below. I need it to skip folders in the directory tree that have the strings test, prototype or _vti in the name. None of the excluded folders are at the top of the tree.

Write-Host  "Looking for new & changed files in $LocalRoot & subfolders"
$exclusions = @("*test*","*prototype*","*_vti*")
Write-Host  "Exclusions: $exclusions`n"
$stopwatch  = [system.diagnostics.stopwatch]::StartNew()
$files      = Get-ChildItem -Path $LocalRoot -Recurse -Attributes Archive -Exclude $exclusions

Before terminating, the script produces this output:

CONFIGURATION DATA:

        LocalRoot=C:/Users/Dick/Documents/tch/site
        ServerRoot=/
        SiteNameInWinSCP=CyberHymnalSFTP

Looking for new & changed files in C:/Users/Dick/Documents/tch/site & subfolders
Exclusions: *test* *prototype* *_vti*
**```**

### Expected behavior

```console
The script should report the number of files found.

Actual behavior

The script aborts with an error after a few minutes

Error details

**Cannot bind parameter 'Property'. Cannot convert the "C:\Users\Dick\Documents\tch\site\img\b\e\e\s\_vti_cnf\beesley_e.jpg" value of type "System.IO.FileInfo" to type "Microsoft.PowerShell.Commands.PSPropertyExpression".**

As you can see, the file is in a folder named **_vti_cnf**, which should have been excluded from the search.

Is this a bug, or am I doing something wrong?

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.4
PSEdition                      Core
GitCommitId                    7.3.4
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.PowerShell-Docs neededThe PR was reviewed and a PowerShell Docs update is neededResolution-AnsweredThe question is answered.Resolution-By DesignThe reported behavior is by design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions