I am working on a Powershell script that will return a list of files based on a filtered set of names and a specific extension, which in this case of the .pdf extension file type. I currently have this set up in a foreach control structure.
So far I have done this:
$MechDWGFilterList = @('*ifc*','*mech*', '*permit*', '*final*', '*CD*')
$MechDWGFile = @()
# $MechDWGFolder contains 1 or more filtered full path files
$MechDWGList = Get-ChildItem -Path $MechDWGFolder -Filter *.pdf -r | Sort-Object -Descending -Property LastWriteTime
$count = 0
foreach ($file in $MechDWGList)
{
# Where the file name contains one of these filters
foreach($filter in $MechDWGFilterList)
{
if($file.Name -like $filter)
{
$MechDWGFile += $file
$count += 1
}
}
}
But I would like to condense something like below where I can avoid having to create an additional object $MechDWGFile from the above code.
$MechDWGFilterList = @('*ifc*','*mech*', '*permit*', '*final*', '*CD*')
$MechDWGFilterList2 = 'ifc|mech|permit|final|CD' #Tried this regex expression as well
$MechDWGList = Get-ChildItem -Path $MechDWGFolder -Filter ($MechDWGFilterList).pdf -r | Sort-Object -Descending -Property LastWriteTime
Write-Output $MechDWGList
I feel like I'm close, unless this is completely not doable without an iterative control loop. Help understanding this challenge would be greatly appreciated!
Some other background refernce info: PowerShell 5.1 is being used as Administrator, Windows 10 OS
-Filterwon't work with regex, you could pass the result fromGet-ChildItemto a filtering cmdlet such asWhere-Object- Santiago Squarzon