Skip to content

Commit 4fc4b25

Browse files
Fixes #12161 - Add note about expandable strings for wildcards (#12169)
* Add note about expandable strings for wildcards * Apply suggestions from review --------- Co-authored-by: Mikey Lombardi (He/Him) <michael.t.lombardi@gmail.com>
1 parent 7008710 commit 4fc4b25

File tree

8 files changed

+375
-263
lines changed

8 files changed

+375
-263
lines changed

reference/5.1/Microsoft.PowerShell.Core/About/about_Comparison_Operators.md

Lines changed: 92 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes the operators that compare values in PowerShell.
33
Locale: en-US
4-
ms.date: 04/30/2025
4+
ms.date: 06/20/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Comparison_Operators
@@ -107,11 +107,11 @@ Example:
107107
```powershell
108108
2 -eq 2 # Output: True
109109
2 -eq 3 # Output: False
110-
"abc" -eq "abc" # Output: True
111-
"abc" -eq "abc", "def" # Output: False
112-
"abc" -ne "def" # Output: True
113-
"abc" -ne "abc" # Output: False
114-
"abc" -ne "abc", "def" # Output: True
110+
'abc' -eq 'abc' # Output: True
111+
'abc' -eq 'abc', 'def' # Output: False
112+
'abc' -ne 'def' # Output: True
113+
'abc' -ne 'abc' # Output: False
114+
'abc' -ne 'abc', 'def' # Output: True
115115
```
116116

117117
When the left-hand side is a collection, `-eq` returns those members that match
@@ -121,14 +121,14 @@ Example:
121121

122122
```powershell
123123
1,2,3 -eq 2 # Output: 2
124-
"abc", "def" -eq "abc" # Output: abc
125-
"abc", "def" -ne "abc" # Output: def
124+
'abc', 'def' -eq 'abc' # Output: abc
125+
'abc', 'def' -ne 'abc' # Output: def
126126
```
127127

128128
These operators process all elements of the collection. Example:
129129

130130
```powershell
131-
"zzz", "def", "zzz" -eq "zzz"
131+
'zzz', 'def', 'zzz' -eq 'zzz'
132132
```
133133

134134
```output
@@ -165,8 +165,8 @@ class MyFileInfoSet {
165165
[string]$File
166166
[int64]$Size
167167
}
168-
$a = [MyFileInfoSet]@{File = "C:\Windows\explorer.exe"; Size = 4651032}
169-
$b = [MyFileInfoSet]@{File = "C:\Windows\explorer.exe"; Size = 4651032}
168+
$a = [MyFileInfoSet]@{File = 'C:\Windows\explorer.exe'; Size = 4651032}
169+
$b = [MyFileInfoSet]@{File = 'C:\Windows\explorer.exe'; Size = 4651032}
170170
$a -eq $b
171171
```
172172

@@ -191,8 +191,8 @@ class MyFileInfoSet : System.IEquatable[Object] {
191191
return ($this.File -eq $obj.File) -and ($this.Size -eq $obj.Size)
192192
}
193193
}
194-
$a = [MyFileInfoSet]@{File = "C:\Windows\explorer.exe"; Size = 4651032}
195-
$b = [MyFileInfoSet]@{File = "C:\Windows\explorer.exe"; Size = 4651032}
194+
$a = [MyFileInfoSet]@{File = 'C:\Windows\explorer.exe'; Size = 4651032}
195+
$b = [MyFileInfoSet]@{File = 'C:\Windows\explorer.exe'; Size = 4651032}
196196
$a -eq $b
197197
```
198198

@@ -270,7 +270,7 @@ Example:
270270
```powershell
271271
$a=5, 6, 7, 8, 9
272272
273-
Write-Output "Test collection:"
273+
Write-Output 'Test collection:'
274274
$a
275275
276276
Write-Output "`nMembers greater than 7"
@@ -348,7 +348,7 @@ raise a non-terminating error.
348348
The matching operators (`-like`, `-notlike`, `-match`, and `-notmatch`) find
349349
elements that match or don't match a specified pattern. The pattern for `-like`
350350
and `-notlike` is a wildcard expression (containing `*`, `?`, and `[ ]`), while
351-
`-match` and `-notmatch` accept a regular expression (Regex).
351+
`-match` and `-notmatch` accept a regular expression (regex).
352352

353353
The syntax is:
354354

@@ -377,15 +377,43 @@ side could be a string containing [wildcards][11].
377377
Example:
378378

379379
```powershell
380-
"PowerShell" -like "*shell" # Output: True
381-
"PowerShell" -notlike "*shell" # Output: False
382-
"PowerShell" -like "Power?hell" # Output: True
383-
"PowerShell" -notlike "Power?hell" # Output: False
384-
"PowerShell" -like "Power[p-w]hell" # Output: True
385-
"PowerShell" -notlike "Power[p-w]hell" # Output: False
380+
'PowerShell' -like '*shell' # Output: True
381+
'PowerShell' -notlike '*shell' # Output: False
382+
'PowerShell' -like 'Power?hell' # Output: True
383+
'PowerShell' -notlike 'Power?hell' # Output: False
384+
'PowerShell' -like 'Power[p-w]hell' # Output: True
385+
'PowerShell' -notlike 'Power[p-w]hell' # Output: False
386386
387-
"PowerShell", "Server" -like "*shell" # Output: PowerShell
388-
"PowerShell", "Server" -notlike "*shell" # Output: Server
387+
'PowerShell', 'Server' -like '*shell' # Output: PowerShell
388+
'PowerShell', 'Server' -notlike '*shell' # Output: Server
389+
```
390+
391+
For best results, the right-hand side of the `-like` and `-notlike` operators
392+
should be a string literal containing the wildcard expression. PowerShell
393+
passes the wildcard expression to the wildcard expression parser. To match one
394+
of the wildcard characters (`*`, `?`, or `[ ]`), you must escape it with a
395+
backtick (`` ` ``) character. For example, to match a literal `?`, use
396+
`` `? `` in the wildcard expression. If you use an expandable string
397+
expression PowerShell expands the string before passing it to the wildcard
398+
parser, which results in unescaped characters being sent as wildcards.
399+
400+
```powershell
401+
# Escaped literals in an expandable string
402+
PS> "f`?`?"
403+
f??
404+
# Escaped literals in a literal string
405+
PS> 'f`?`?'
406+
f`?`?
407+
# Comparison containing 2 wildcards
408+
PS> 'f??' -like 'f??'
409+
True
410+
PS> 'for' -like 'f??'
411+
True
412+
# Comparison containing literal '?' characters
413+
PS> 'f??' -like 'f`?`?'
414+
True
415+
PS> 'for' -like 'f`?`?'
416+
False
389417
```
390418

391419
### -match and -notmatch
@@ -399,11 +427,11 @@ Scalar examples:
399427

400428
```powershell
401429
# Partial match test, showing how differently -match and -like behave
402-
"PowerShell" -match 'shell' # Output: True
403-
"PowerShell" -like 'shell' # Output: False
430+
'PowerShell' -match 'shell' # Output: True
431+
'PowerShell' -like 'shell' # Output: False
404432
405433
# Regex syntax test
406-
"PowerShell" -match '^Power\w+' # Output: True
434+
'PowerShell' -match '^Power\w+' # Output: True
407435
'bag' -notmatch 'b[iou]g' # Output: True
408436
```
409437

@@ -413,16 +441,16 @@ collection.
413441
Collection examples:
414442

415443
```powershell
416-
"PowerShell", "Super PowerShell", "Power's hell" -match '^Power\w+'
444+
'PowerShell', 'Super PowerShell', 'Power's hell' -match '^Power\w+'
417445
# Output: PowerShell
418446
419-
"Rhell", "Chell", "Mel", "Smell", "Shell" -match "hell"
447+
'Rhell', 'Chell', 'Mel', 'Smell', 'Shell' -match 'hell'
420448
# Output: Rhell, Chell, Shell
421449
422-
"Bag", "Beg", "Big", "Bog", "Bug" -match 'b[iou]g'
450+
'Bag', 'Beg', 'Big', 'Bog', 'Bug' -match 'b[iou]g'
423451
#Output: Big, Bog, Bug
424452
425-
"Bag", "Beg", "Big", "Bog", "Bug" -notmatch 'b[iou]g'
453+
'Bag', 'Beg', 'Big', 'Bog', 'Bug' -notmatch 'b[iou]g'
426454
#Output: Bag, Beg
427455
```
428456

@@ -477,7 +505,7 @@ operator invocation using a condition statement.
477505
Example:
478506

479507
```powershell
480-
if ("<version>1.0.0</version>" -match '<version>(.*?)</version>') {
508+
if ('<version>1.0.0</version>' -match '<version>(.*?)</version>') {
481509
$Matches
482510
}
483511
```
@@ -515,8 +543,8 @@ sensitive, use `-creplace`. To make it explicitly case-insensitive, use
515543
Examples:
516544

517545
```powershell
518-
"book" -ireplace "B", "C" # Case insensitive
519-
"book" -creplace "B", "C" # Case-sensitive; hence, nothing to replace
546+
'book' -ireplace 'B', 'C' # Case insensitive
547+
'book' -creplace 'B', 'C' # Case-sensitive; hence, nothing to replace
520548
```
521549

522550
```Output
@@ -561,7 +589,7 @@ For example:
561589
```powershell
562590
$1 = 'Goodbye'
563591
564-
'Hello World' -replace '(\w+) \w+', "$1 Universe"
592+
'Hello World' -replace '(\w+) \w+', '$1 Universe'
565593
# Output: Goodbye Universe
566594
567595
'Hello World' -replace '(\w+) \w+', '$1 Universe'
@@ -586,7 +614,7 @@ When the `<input>` to the `-replace` operator is a collection, PowerShell
586614
applies the replacement to every value in the collection. For example:
587615

588616
```powershell
589-
"B1","B2","B3","B4","B5" -replace "B", 'a'
617+
'B1','B2','B3','B4','B5' -replace 'B', 'a'
590618
a1
591619
a2
592620
a3
@@ -619,20 +647,20 @@ elements in the set. `-notcontains` returns False instead.
619647
Examples:
620648

621649
```powershell
622-
"abc", "def" -contains "def" # Output: True
623-
"abc", "def" -notcontains "def" # Output: False
624-
"Windows", "PowerShell" -contains "Shell" # Output: False
625-
"Windows", "PowerShell" -notcontains "Shell" # Output: True
626-
"abc", "def", "ghi" -contains "abc", "def" # Output: False
627-
"abc", "def", "ghi" -notcontains "abc", "def" # Output: True
650+
'abc', 'def' -contains 'def' # Output: True
651+
'abc', 'def' -notcontains 'def' # Output: False
652+
'Windows', 'PowerShell' -contains 'Shell' # Output: False
653+
'Windows', 'PowerShell' -notcontains 'Shell' # Output: True
654+
'abc', 'def', 'ghi' -contains 'abc', 'def' # Output: False
655+
'abc', 'def', 'ghi' -notcontains 'abc', 'def' # Output: True
628656
```
629657

630658
More complex examples:
631659

632660
```powershell
633-
$DomainServers = "ContosoDC1", "ContosoDC2", "ContosoFileServer",
634-
"ContosoDNS", "ContosoDHCP", "ContosoWSUS"
635-
$thisComputer = "ContosoDC2"
661+
$DomainServers = 'ContosoDC1', 'ContosoDC2', 'ContosoFileServer',
662+
'ContosoDNS', 'ContosoDHCP', 'ContosoWSUS'
663+
$thisComputer = 'ContosoDC2'
636664
637665
$DomainServers -contains $thisComputer
638666
# Output: True
@@ -643,13 +671,13 @@ value to its string representation before comparing it to the left-hand side
643671
collection.
644672

645673
```powershell
646-
$a = "abc", "def"
647-
"abc", "def", "ghi" -contains $a # Output: False
674+
$a = 'abc', 'def'
675+
'abc', 'def', 'ghi' -contains $a # Output: False
648676
649677
# The following statements are equivalent
650-
$a, "ghi" -contains $a # Output: True
651-
"$a", "ghi" -contains $a # Output: True
652-
"abc def", "ghi" -contains $a # Output: True
678+
$a, 'ghi' -contains $a # Output: True
679+
'$a', 'ghi' -contains $a # Output: True
680+
'abc def', 'ghi' -contains $a # Output: True
653681
```
654682

655683
### -in and -notin
@@ -670,20 +698,20 @@ The following examples do the same thing that the examples for `-contains` and
670698
`-notcontains` do, but they're written with `-in` and `-notin` instead.
671699

672700
```powershell
673-
"def" -in "abc", "def" # Output: True
674-
"def" -notin "abc", "def" # Output: False
675-
"Shell" -in "Windows", "PowerShell" # Output: False
676-
"Shell" -notin "Windows", "PowerShell" # Output: True
677-
"abc", "def" -in "abc", "def", "ghi" # Output: False
678-
"abc", "def" -notin "abc", "def", "ghi" # Output: True
701+
'def' -in 'abc', 'def' # Output: True
702+
'def' -notin 'abc', 'def' # Output: False
703+
'Shell' -in 'Windows', 'PowerShell' # Output: False
704+
'Shell' -notin 'Windows', 'PowerShell' # Output: True
705+
'abc', 'def' -in 'abc', 'def', 'ghi' # Output: False
706+
'abc', 'def' -notin 'abc', 'def', 'ghi' # Output: True
679707
```
680708

681709
More complex examples:
682710

683711
```powershell
684-
$DomainServers = "ContosoDC1", "ContosoDC2", "ContosoFileServer",
685-
"ContosoDNS", "ContosoDHCP", "ContosoWSUS"
686-
$thisComputer = "ContosoDC2"
712+
$DomainServers = 'ContosoDC1', 'ContosoDC2', 'ContosoFileServer',
713+
'ContosoDNS', 'ContosoDHCP', 'ContosoWSUS'
714+
$thisComputer = 'ContosoDC2'
687715
688716
$thisComputer -in $DomainServers
689717
# Output: True
@@ -694,13 +722,13 @@ value to its string representation before comparing it to the right-hand side
694722
collection.
695723

696724
```powershell
697-
$a = "abc", "def"
698-
$a -in "abc", "def", "ghi" # Output: False
725+
$a = 'abc', 'def'
726+
$a -in 'abc', 'def', 'ghi' # Output: False
699727
700728
# The following statements are equivalent
701-
$a -in $a, "ghi" # Output: True
702-
$a -in "$a", "ghi" # Output: True
703-
$a -in "abc def", "ghi" # Output: True
729+
$a -in $a, 'ghi' # Output: True
730+
$a -in '$a', 'ghi' # Output: True
731+
$a -in 'abc def', 'ghi' # Output: True
704732
```
705733

706734
## Type comparison
@@ -719,7 +747,7 @@ Example:
719747

720748
```powershell
721749
$a = 1
722-
$b = "1"
750+
$b = '1'
723751
$a -is [int] # Output: True
724752
$a -is $b.GetType() # Output: False
725753
$b -isnot [int] # Output: True

reference/5.1/Microsoft.PowerShell.Core/About/about_Operators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ determine whether they're equal.
5050

5151
The comparison operators also include operators that find or replace patterns
5252
in text. The (`-match`, `-notmatch`, `-replace`) operators use regular
53-
expressions, and (`-like`, `-notlike`) use wildcards like `*`.
53+
expressions, and (`-like`, `-notlike`) use wildcards like `*` and `?`.
5454

5555
Containment comparison operators determine whether a test value appears in a
5656
reference set (`-in`, `-notin`, `-contains`, `-notcontains`).

0 commit comments

Comments
 (0)