Powershell - LIKE against an array
You can perform a pattern match against a collection of names, but not against a list of patterns. Try this:
foreach($pattern in $excludeFiles)
{
if($fileName -like $pattern) {break}
}
Here is how it can be wrapped into a function:
function like($str,$patterns){
foreach($pattern in $patterns) { if($str -like $pattern) { return $true; } }
return $false;
}
Here is a short version of the pattern in the accepted answer:
($your_array | %{"your_string" -like $_}) -contains $true
Applied to the case in the OP one obtains
PS C:\> ("aaa.bbb", "ccccc.*", "ddddd???.exe" | %{"ddddd000.exe" -like $_}) -contains $true
True
I suppose you could use the Get-ChildItem
-Exclude
parameter:
Get-ChildItem $theFileToCheck -exclude $excludeFiles
If you have an array of files to check, Get-ChildItem
accepts an array of paths:
Get-ChildItem $filesToCheck -exclude $excludeFiles