October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

PowerShell -contains vs. -like: Membership and Wildcard Matching

PowerShell -contains checks collection membership; -like matches strings against wildcard patterns. See how operand order, case, and collection results differ.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use -contains to ask whether a collection includes an exact value; use -like to test a string against a wildcard pattern. They solve different problems: -contains is a membership check, while -like is a pattern match.

What is the difference between -contains and -like?

-contains checks whether the collection on its left includes the value on its right and returns $true or $false. -like compares a string on its left with a wildcard expression on its right.

As an Amazon Associate I earn from qualifying purchases.

For example, 'PowerShell' -like '*shell' is true because the wildcard can match the characters before shell. But 'PowerShell' -contains 'shell' is false: the left operand is not a collection containing an element equal to shell. Microsoft documents these operators in its comparison-operator reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When should you use each operator?

Use -contains for exact collection membership

Put the collection on the left and the value to look for on the right. The question is: “Is this value one of the collection’s elements?”

@('abc', 'def') -contains 'def'

This returns $true because def is an element. The operand order matters: -contains is not an infix operator for checking whether text contains a substring.

You can express the same membership question with -in and reverse the operands: 'def' -in @('abc', 'def').

Use -like for wildcard string patterns

Put the string on the left and a wildcard expression on the right. The question is: “Does this string fit this text pattern?”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
'PowerShell' -like '*shell'

This returns $true because * can match a sequence of characters. If you need exact scalar string equality rather than a pattern, use -eq.

What do the wildcard characters mean?

PowerShell wildcard patterns are not regular expressions. The main wildcard characters are:

  • * matches a sequence of characters, including an empty sequence.
  • ? matches one character.
  • [ ] can specify character alternatives or ranges.

For example, 'PowerShell' -like 'Power?hell' is true because the question mark matches one character. To match a wildcard character literally in a pattern, escape it with a backtick. Microsoft’s wildcard reference describes the pattern syntax and recommends using a string literal for the right-hand pattern; expandable strings can interpolate content before wildcard parsing.

Rank #4
Sale
PowerShell for Sysadmins: Workflow Automation Made Easy
  • Book - powershell for sysadmins: workflow automation made easy
  • Language: english
  • Binding: paperback

How do results change when the left operand is a collection?

Do not assume every comparison operator returns one Boolean when its left operand is a collection. The output depends on the operator:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • -contains always returns a Boolean and stops after finding a match.
  • -like filters a collection and returns the matching members as strings. It does not return one overall Boolean for the collection.

This distinction is especially useful in scripts: use -contains when you need a yes-or-no membership answer, and use -like when you want the items whose text matches a pattern. Microsoft’s comparison-operator reference documents this collection behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Are these operators case-sensitive?

The ordinary string-comparison forms are case-insensitive. Add c after the hyphen to make the comparison case-sensitive, or use i to make case-insensitivity explicit:

  • -contains and -icontains are case-insensitive; -ccontains is case-sensitive.
  • -like and -ilike are case-insensitive; -clike is case-sensitive.

For example, 'PowerShell' -clike '*shell' performs case-sensitive wildcard matching. The PowerShell language specification says the ordinary -<operator> form is equivalent to the -i<operator> form for these string comparisons.

Which one should you choose?

Need Example What it checks
Check exact collection membership @('abc','def') -contains 'def' Whether def is an element; returns a Boolean.
Match a suffix pattern 'PowerShell' -like '*shell' Whether the string ends in shell, with any prefix.
Match one character 'PowerShell' -like 'Power?hell' Whether one character fits the position marked by ?.
Make wildcard matching case-sensitive 'PowerShell' -clike '*shell' Whether the string matches the wildcard pattern with case respected.

The basic distinction is covered by Microsoft’s comparison reference for Windows PowerShell 5.1 and its PowerShell 7.6 comparison tutorial; the wildcard and language-specification references are for PowerShell 7.5. Check documentation for the PowerShell version you use if you are investigating version-specific behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a general overview of comparisons, see Microsoft’s comparison-operator reference and PowerShell 101 comparison tutorial.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.