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?
Sort-Object, you're not really sorting by the LastWriteTime property since you're only outputting the fullname to the pipeline.C:\Users\USERNAME\Box\CTRL Active Projects\project1\project1_OPS\01-Eng\03-Submittals WIP\Backupseven though the-Excludeis listing*eng*, *EDS*(2) The other option instead of using-Filterwas to see how-Includeworks with-Excludebut so far the results have been the same. (3) The goal withSort-Objectwas to capture and list in order an array of folders that included the the words 'backup' in the name somewhere and exclude 'EDS' and 'ENG' in its name.