'How to filter include folders in Powershell while excluding others

I am wanting to filter through a list of folders that include a certain criteria while excluding another set of criteria in PowerShell.

The code below is what is currently being played around with that has had partial succession; -Exclude seems to exclude folders that contain *EDS* in the name but not *eng*. Below is a sample output of what is still being included:

C:\Users\USERNAME\Box\CTRL Active Projects\project1\project1_OPS\06-Software\04-Database Backups\01-Record Backup
C:\Users\USERNAME\Box\CTRL Active Projects\project1\project1_OPS\06-Software\04-Database Backups
C:\Users\USERNAME\Box\CTRL Active Projects\project1\project1_OPS\01-Eng\03-Submittals WIP\Backups

The -Exclude *eng*, *EDS* might eventually be converted over to a variable/named arraylist.

# Find sub-folder within job folder
$DatabaseFolder = (Get-ChildItem -Path $JobFolder -Filter "*Backup*" -Exclude *eng*, *EDS* -Recurse -Directory).Fullname | Sort-Object -Descending -Property LastWriteTime
Write-Output $DatabaseFolder

The $JobFolder returns the path to search the sub directories:

C:\Users\USERNAME\Box\CTRL Active Projects\project1\

Some other background info: PowerShell 5.1 is being used as Administrator, Windows 10 OS

Could someone help me understand why the code is still including some of the excluded parameters?



Solution 1:[1]

I don't see anything in the documentation about using Filter and Exclude:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.2

However, since it's not working as you intended, you can always manually perform the exclude:

gci -recurse -directory | ? { $_.Name -notlike "*eng*" -and $_.Name -notlike "*EDS*" }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 thepip3r