1
1
---
2
2
description : Describes the operators that compare values in PowerShell.
3
3
Locale : en-US
4
- ms.date : 04/30 /2025
4
+ ms.date : 06/20 /2025
5
5
online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-5.1&WT.mc_id=ps-gethelp
6
6
schema : 2.0.0
7
7
title : about_Comparison_Operators
@@ -107,11 +107,11 @@ Example:
107
107
``` powershell
108
108
2 -eq 2 # Output: True
109
109
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
115
115
```
116
116
117
117
When the left-hand side is a collection, ` -eq ` returns those members that match
@@ -121,14 +121,14 @@ Example:
121
121
122
122
``` powershell
123
123
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
126
126
```
127
127
128
128
These operators process all elements of the collection. Example:
129
129
130
130
``` powershell
131
- " zzz", " def", " zzz" -eq " zzz"
131
+ ' zzz', ' def', ' zzz' -eq ' zzz'
132
132
```
133
133
134
134
``` output
@@ -165,8 +165,8 @@ class MyFileInfoSet {
165
165
[string]$File
166
166
[int64]$Size
167
167
}
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}
170
170
$a -eq $b
171
171
```
172
172
@@ -191,8 +191,8 @@ class MyFileInfoSet : System.IEquatable[Object] {
191
191
return ($this.File -eq $obj.File) -and ($this.Size -eq $obj.Size)
192
192
}
193
193
}
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}
196
196
$a -eq $b
197
197
```
198
198
@@ -270,7 +270,7 @@ Example:
270
270
``` powershell
271
271
$a=5, 6, 7, 8, 9
272
272
273
- Write-Output " Test collection:"
273
+ Write-Output ' Test collection:'
274
274
$a
275
275
276
276
Write-Output "`nMembers greater than 7"
@@ -348,7 +348,7 @@ raise a non-terminating error.
348
348
The matching operators (` -like ` , ` -notlike ` , ` -match ` , and ` -notmatch ` ) find
349
349
elements that match or don't match a specified pattern. The pattern for ` -like `
350
350
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 ).
352
352
353
353
The syntax is:
354
354
@@ -377,15 +377,43 @@ side could be a string containing [wildcards][11].
377
377
Example:
378
378
379
379
``` 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
386
386
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
389
417
```
390
418
391
419
### -match and -notmatch
@@ -399,11 +427,11 @@ Scalar examples:
399
427
400
428
``` powershell
401
429
# 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
404
432
405
433
# Regex syntax test
406
- " PowerShell" -match '^Power\w+' # Output: True
434
+ ' PowerShell' -match '^Power\w+' # Output: True
407
435
'bag' -notmatch 'b[iou]g' # Output: True
408
436
```
409
437
@@ -413,16 +441,16 @@ collection.
413
441
Collection examples:
414
442
415
443
``` powershell
416
- " PowerShell", " Super PowerShell", " Power's hell" -match '^Power\w+'
444
+ ' PowerShell', ' Super PowerShell', ' Power's hell' -match '^Power\w+'
417
445
# Output: PowerShell
418
446
419
- " Rhell", " Chell", " Mel", " Smell", " Shell" -match " hell"
447
+ ' Rhell', ' Chell', ' Mel', ' Smell', ' Shell' -match ' hell'
420
448
# Output: Rhell, Chell, Shell
421
449
422
- " Bag", " Beg", " Big", " Bog", " Bug" -match 'b[iou]g'
450
+ ' Bag', ' Beg', ' Big', ' Bog', ' Bug' -match 'b[iou]g'
423
451
#Output: Big, Bog, Bug
424
452
425
- " Bag", " Beg", " Big", " Bog", " Bug" -notmatch 'b[iou]g'
453
+ ' Bag', ' Beg', ' Big', ' Bog', ' Bug' -notmatch 'b[iou]g'
426
454
#Output: Bag, Beg
427
455
```
428
456
@@ -477,7 +505,7 @@ operator invocation using a condition statement.
477
505
Example:
478
506
479
507
``` powershell
480
- if (" <version>1.0.0</version>" -match '<version>(.*?)</version>') {
508
+ if (' <version>1.0.0</version>' -match '<version>(.*?)</version>') {
481
509
$Matches
482
510
}
483
511
```
@@ -515,8 +543,8 @@ sensitive, use `-creplace`. To make it explicitly case-insensitive, use
515
543
Examples:
516
544
517
545
``` 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
520
548
```
521
549
522
550
``` Output
@@ -561,7 +589,7 @@ For example:
561
589
``` powershell
562
590
$1 = 'Goodbye'
563
591
564
- 'Hello World' -replace '(\w+) \w+', " $1 Universe"
592
+ 'Hello World' -replace '(\w+) \w+', ' $1 Universe'
565
593
# Output: Goodbye Universe
566
594
567
595
'Hello World' -replace '(\w+) \w+', '$1 Universe'
@@ -586,7 +614,7 @@ When the `<input>` to the `-replace` operator is a collection, PowerShell
586
614
applies the replacement to every value in the collection. For example:
587
615
588
616
``` powershell
589
- "B1","B2","B3","B4","B5" -replace "B" , 'a'
617
+ 'B1','B2','B3','B4','B5' -replace 'B' , 'a'
590
618
a1
591
619
a2
592
620
a3
@@ -619,20 +647,20 @@ elements in the set. `-notcontains` returns False instead.
619
647
Examples:
620
648
621
649
``` 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
628
656
```
629
657
630
658
More complex examples:
631
659
632
660
``` 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'
636
664
637
665
$DomainServers -contains $thisComputer
638
666
# Output: True
@@ -643,13 +671,13 @@ value to its string representation before comparing it to the left-hand side
643
671
collection.
644
672
645
673
``` 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
648
676
649
677
# 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
653
681
```
654
682
655
683
### -in and -notin
@@ -670,20 +698,20 @@ The following examples do the same thing that the examples for `-contains` and
670
698
` -notcontains ` do, but they're written with ` -in ` and ` -notin ` instead.
671
699
672
700
``` 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
679
707
```
680
708
681
709
More complex examples:
682
710
683
711
``` 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'
687
715
688
716
$thisComputer -in $DomainServers
689
717
# Output: True
@@ -694,13 +722,13 @@ value to its string representation before comparing it to the right-hand side
694
722
collection.
695
723
696
724
``` 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
699
727
700
728
# 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
704
732
```
705
733
706
734
## Type comparison
@@ -719,7 +747,7 @@ Example:
719
747
720
748
``` powershell
721
749
$a = 1
722
- $b = "1"
750
+ $b = '1'
723
751
$a -is [int] # Output: True
724
752
$a -is $b.GetType() # Output: False
725
753
$b -isnot [int] # Output: True
0 commit comments