To deal with your problem and all similar existential quantification related problems, we might want a generic solution that allows us to do something like:
PS> "aaa.bbb", "ccccc.*", "ddddd???.exe"| any { "ddddd000.exe" -like $_ }
True
PS> "aaa.bbb", "ccccc.*", "ddddd???.exe"| any { "ddd000.exe" -like $_ }
False
Solution: Utility function Test-Any
Simply put this function in your (profile) scripts:
function Test-Any {
<#
.SYNOPSIS
Check if any element satisfies a predicate, returns a boolean.
.PARAMETER InputObject
Prefer using pipeline over parameter.
Scalar argument provided through parameter will be wrapped into an array.
.PARAMETER Predicate
ScriptBlock function: ([Object]$Element) -> [Boolean]$Result
.EXAMPLE
Test-Any -InputObject 1,2,3,4 {$_ % 2}
True
.EXAMPLE
1,(2,3),4 | Test-Any {$_.Count -eq 3}
False
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)][AllowNull()]
[Object]$InputObject,
[Parameter(Mandatory, Position = 0)][ArgumentCompletions('{}')]
[ScriptBlock]$Predicate
)
# Replace `$InputObject` with pipeline value, if value is provided from pipeline.
if ($MyInvocation.ExpectingInput) { $InputObject = $input }
else { $InputObject = @($InputObject) }
foreach ($o in $InputObject) {
if ($Predicate.InvokeWithContext($null, [PSVariable]::new('_', $o))) {
return $true
}
}
return $false
}
I would also recommend adding the alias any for ease of use:
New-Alias -Name any -Value Test-Any
Bonus: Utility function Test-All
Might as well have a function for universal quantification too!
function Test-All {
<#
.SYNOPSIS
Check if all elements satisfy a predicate, returns a boolean.
.PARAMETER InputObject
Prefer using pipeline over parameter.
Scalar argument provided through parameter will be wrapped into an array.
.PARAMETER Predicate
ScriptBlock function: ([Object]$Element) -> [Boolean]$Result
.EXAMPLE
1,2,3,4 | Test-All {$_ % 2}
False
.EXAMPLE
1,3,5 | Test-All {$_ % 2}
True
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)][AllowNull()]
[Object]$InputObject,
[Parameter(Mandatory, Position = 0)][ArgumentCompletions('{}')]
[ScriptBlock]$Predicate
)
# Replace `$InputObject` with pipeline value, if value is provided from pipeline.
if ($MyInvocation.ExpectingInput) { $InputObject = $input }
else { $InputObject = @($InputObject) }
foreach ($o in $InputObject) {
if (!$Predicate.InvokeWithContext($null, [PSVariable]::new('_', $o))) {
return $false
}
}
return $true
}
(These functions and their naming are inspired by the ones in Kotlin standard library, any and all).
"aaa.bbb ccccc.* ddddd???.exe". Btw you don't need@( )-match "aaa.bbb|ccccc.|ddddd....exe"