Home > database >  Powershell how to filter Get-ChildItem with a multiple values
Powershell how to filter Get-ChildItem with a multiple values

Time:08-11

I need to get-children items a directory and find to a specific named zip file.

I don't know to zip file's name. But one thing that I know the zip file includes a date. And I have to find full path of zip from that date. I mean like:

   xxxx_20220810.zip
   or
   xxxx20220810.zip

I check this topic powershell Get-ChildItem given multiple -Filters but this page doesn't help me.

   $date = (Get-Date).ToString('yyyyMMdd')
   $zipPath = Get-ChildItem  -Path $path -Filter '*.zip'  -Recurse| % { $_.FullName -match $date}
   $zipPath = Get-ChildItem  -Path $path -Filter '*.zip'  -Recurse| % { $_.FullName -contains $date}

CodePudding user response:

What you're asking for can still be expressed as a single filter expression:

$date = (Get-Date).ToString('yyyyMMdd')
$zipPath = Get-ChildItem -Path $path -Filter "*${date}*.zip" -Recurse

-Filter only accepts a single pattern, so if you do need to filter against any of a number of conflicting patterns, you'll still have to use Where-Object:

Get-ChildItem -Path $path -Filter *.zip -Recurse |Where-Object {
   $_.BaseName -like "*${year}*${month}*${day}*" -or $_.BaseName -like "*${year}*${day}*${month}*"
}
  • Related