Repository navigation
Closed
Labels
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugNeeds-TriageThe issue is new and needs to be triaged by a work group.Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics
Description
Steps to reproduce
Imagine having a list of computer names from AD, like ABC0001, ABC0002, DEF0001, XYZ0001 and so on, and you only want to keep ABC* and XYZ*.
Expected behavior
We want to filter so we'll use -Like, and try to use an array of comparison values.
Get-ADComputer | Where-Object ComputerName -like ABC*, XYZ*
ABC0001
ABC0002
XYZ0001
...
Actual behavior
Single comparisons work
Get-ADComputer | Where-Object ComputerName -like ABC*
ComputerName
------------
ABC0001
ABC0002
ABC0003
ABC0004
ABC0005
Get-ADComputer | Where-Object ComputerName -like XYZ*
ComputerName
------------
XYZ0001
XYZ0002
XYZ0003
XYZ0004
XYZ0005
But combining them does not work
Where-Object ComputerName -like ABC*, XYZ*
PS>
With PowerShell being object based and having such a uniform syntax, it feels like this should work, so people tend to be confused (like me) and when doing really complex filters, write garbage like this:
Or read from an arcane tome of RegEx Spells and come up with devilry like this example, from the wizard Dave Wyatt.
? { $_.e -match '^(atl|na|mdm|aus|okc|ssc|ga1|ok1|fns|acs)' }
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14393.103
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.103
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Reacted by Zhiqiang Yu, Raghav S, mazzy, Michael Klement, MartinGC94, winston henke, iRon7, RockoTheeHut and Olav Rønnestad Birkeland
Metadata
Metadata
Assignees
Labels
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugNeeds-TriageThe issue is new and needs to be triaged by a work group.Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

Activity
vors commentedon Aug 30, 2016
I usually write it in this form to avoid repeating
-andand-notlikelzybkr commentedon Aug 30, 2016
Changing the meaning of arrays could be a (probably rare but obscure) breaking change.
A new operator, e.g.
-likeanymight be more readable. Note that these examples are similar to part of the generalized splatting rfc, which actually suggest another syntax that wouldn't be breaking:$a -like @b.GeeLaw commentedon Sep 1, 2016
For now, you can workaround this with
-NotMatchand-Match. Instead of usingYou can write
SteveL-MSFT commentedon Nov 1, 2016
This should be submitted as an RFC
HumanEquivalentUnit commentedon Oct 3, 2017
It does work, it's telling you that you don't have a computer called
"abc1, xyz2"or similar.In this example
-likeis not an operator, it's a parameter ofWhere-Objectso the pattern is parsed in parameter parsing mode. It becomes a single string pattern containing a comma and a space. It matches text "abc anything comma space xyz anything":That is, it does work in line with the uniform syntax, it's just misleading you about which syntax applies.
This is different from the operator used with quotes
-like "abc*", "xyz*"which is parsed as lzybkr describes - an array joined together using the value of$OFSas the separator (space by default), so the comma doesn't become a part of the pattern but a space does.And with the operator, trying to use the example with no quotes
@('zzz', 'abc1', 'xyz2') -like abc*, xyz*is a parse error.If the suggested
-likeanybecame a parameter onwhere-objectthen this slightly misleading pattern would apply to it in the same way:And the original issue would still be here:
Where-Object test -likeany abc*, def*doesn't work as expected, because the expectation was not thinking about parameter parsing mode.Presumably with a new operator there would be variants
-clikeany,-ilikeany?What about
-notlikeany,-cnotlikeany,-inotlikeany?Should that pattern extend to
-matchany,-notmatchany,-containsany,-notcontainsany,-anyin,-notanyin) and their case variants?-likeall?If not, then you get the weird situation where
-likeanybecomes the de-facto-containsany:I would vote for a breaking change to make it work with
-like, personally. Most, if not all, the existing operators work with an array on one side or the other, without needing a variant on the name specifically for the case of working with an array. And possibly adjust-contains @('a', 'b')similarly, so it doesn't join the array.I see the original issue - matching multiple patterns - appears over and over on StackOverflow as a thing people trip over, expecting there to be a clean way and there isn't. This seems like a helpful fix for that. But..
In defence of regex, it is a language specifically for describing patterns in text, and the use of
^,()and|is not as arcane and devilish as claimed; regex is a first class citizen in PowerShell already - in common operators like-splitand-replace, and it's available in every popular language, many popular text editors, and many other data processing tools.At what point is it reasonable to say "honestly, your next move should be: spend an hour with a regex tutorial, it's really not that bad"? Is there anything that can be done to make it less intimidating?
mklement0 commentedon Oct 17, 2018
I agree, given that:
this strikes me as a clear-cut Bucket 3: Unlikely Grey Area change
it avoids introducing yet another operator
extending the RHS to support arrays seems like a natural extension of the existing functionality.
Depending on your requirements, sometimes regexes are your only choice.
But even if you have a decent grasp of regexes, it is convenient to use the less noisy and conceptually simpler wildcard patterns in cases where that is enough - which is often.
Wildcards are much more pervasive in PowerShell (think parameters that support them; e.g.
Get-Process ba*) and therefore a familiar feature.That said, we could similarly introduce array RHS support for the
-matchoperator as well:To me, there's something cleaner about writing
than (what I propose the above would be the equivalent of):
For symmetry we'd have to add this to the potential future
-matchalloperator as well - see #7867; though perhaps the naming of that new operator should be revisited in this context.On a side note, HumanEquivalentUnit:
I get the difference in parsing modes (of argument-mode
ABC*, XYZ*vs. expression-mode'ABC*', 'XYZ*'), but, quoting aside, both are parsed as arrays that are converted to a single string in which the elements are concatenated with$OFS, which yields string'ABC* XYZ*'by default.iRon7 commentedon Aug 14, 2022
I very much agree with HumanEquivalentUnit as this issue might even qualify as a bug if you strictly take the common features of the comparison operators:
Meaning, according to the documentation, this:
'a', 'b' -in 'b', 'c'which currently returns a Boolean ($false), should actually return the elements of the collection that match the right-hand value of the expression.Needless to say:
$a -eq $b(where$acould be a collection), is in fact similar to$a -contains $b(and$b -in $a, where the operators are swapped) with the exception that$a -eq $breturns a truty/falsy (which could be more useful) and the later comparisons return an explicit Boolean ($True/$False).iRon7 commentedon Aug 15, 2022
HumanEquivalentUnit
Just a thought: maybe using the [unary comma
,] to a "force" the RHS to an array:Something like:
or:
(It might still break a few things, but a probably a lot less)
iRon7 commentedon Nov 16, 2023
This issue Needs-Triage and either marked with something like Resolution-Declined or put it on a kind of wishlist "Issue we would like to prioritize, but we can't commit we will get to it yet"
iRon7 commentedon Aug 29, 2024
In case performance is key, you might consider using the
.wheremethod instead of theWhere-Objectcmdlet: