Free tools Windows power users keep installed

One-click scans. No signup required.

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

-contains and -like answer different questions in PowerShell. Use -contains to test whether a collection has an exact value; use -like to match text against a wildcard pattern. For example, 'PowerShell' -like '*shell' is $true, while 'PowerShell' -contains 'shell' is $false.

What each operator does

The practical distinction is membership versus pattern matching:

  • -contains asks whether the left-hand collection contains an element equal to the right-hand value.
  • -like asks whether the left-hand string fits a wildcard expression on the right.

They are not interchangeable. -contains does not search for a substring, and -like is not an exact collection-membership test.

Syntax and return values

-contains: collection on the left

<Collection> -contains <scalar-object>

Containment operators always return one Boolean value: $true or $false. They stop once a matching element is found.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$names = @('Ada', 'Grace', 'Linus')
$names -contains 'Grace'   # $true
$names -contains 'grace'   # $true (ordinary form is case-insensitive)
$names -contains 'Alan'    # $false

The comparison is against whole elements. With $names, the value 'ra' is not an element, so it is not a match even though those characters occur inside 'Grace'.

-like: string and wildcard expression

<string> -like <wildcard-expression>

With a scalar left operand, -like returns a Boolean:

'PowerShell' -like '*shell'      # $true
'PowerShell' -like 'Power?hell'   # $true
'PowerShell' -like 'Power'        # $false

The pattern must describe the complete value unless wildcards allow additional characters. A leading or trailing * is therefore commonly used for “contains this text” or prefix/suffix checks.

Choosing the right operator

Need Expression Meaning and result
Check exact membership @('abc','def') -contains 'def' $true, because def is an element.
Match a suffix 'PowerShell' -like '*shell' $true; * can match the prefix.
Match one character 'PowerShell' -like 'Power?hell' $true; ? matches one character.
Require exact scalar equality $value -eq 'PowerShell' Use equality when no wildcard pattern is needed.
Use the same membership test with reversed operands 'def' -in @('abc','def') The -in form asks whether the left scalar is in the right collection.

If your question is “Is this exact value one of these items?”, use -contains (or -in). If it is “Does this text fit this pattern?”, use -like. For exact comparison of two scalar values, use -eq.

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

What happens when the left side is a collection?

PowerShell comparison operators do not all produce the same shape of output when the left operand is an array.

Containment remains Boolean

@('app.log', 'app.txt') -contains 'app.log'
# $true

-contains, -notcontains, -in, and -notin always produce a single Boolean, even when the input is a collection.

-like filters and returns matching members

@('app.log', 'app.txt', 'readme.md') -like '*.log'
# app.log

When -like receives a collection on the left, PowerShell applies the pattern to each element and returns the matching members (as strings), not one overall $true or $false. If nothing matches, the result is no output (an empty result), which is different from a Boolean $false.

This behavior also applies to other comparison operators such as -eq: do not assume that every comparison against an array is a true/false test.

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

Case sensitivity

The ordinary string comparison forms are case-insensitive. Their explicit prefixes make the intent clear:

Rank #4
Sale
PowerShell for Sysadmins: Workflow Automation Made Easy
  • Book - powershell for sysadmins: workflow automation made easy
  • Language: english
  • Binding: paperback
  • -contains and -like are case-insensitive by default.
  • Use -ccontains or -clike for case-sensitive comparison.
  • Use -icontains or -ilike when you want to state case-insensitivity explicitly.
'PowerShell' -clike '*shell'  # $false
'PowerShell' -clike '*Shell'  # $true
@('Admin','admin') -ccontains 'admin' # $true (matches only lowercase element)

The c and i prefixes are useful in shared scripts because they document whether capitalization is significant instead of relying on the default.

PowerShell wildcard rules for -like

-like uses PowerShell wildcard syntax, not regular expressions:

  • * matches zero or more characters.
  • ? matches exactly one character.
  • [ ] specifies character alternatives or ranges.
'cat' -like 'c[ab]t'   # $true
'file7' -like 'file[0-9]' # $true

Matching a wildcard literally

Escape a wildcard character with a backtick when the character itself must be present in the text:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
'100% complete *' -like '*`*'  # $true: the final asterisk is literal

Use a string literal for the right-hand pattern when possible. An expandable double-quoted string can interpolate variables before PowerShell interprets the wildcard expression, which may change the pattern you intended.

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

Common mistakes and fixes

Using -contains for a substring

'PowerShell' -contains 'shell'  # $false

The left operand is a scalar string here, and -contains is asking for collection membership rather than searching inside the text. For a substring-style test, use a wildcard:

'PowerShell' -like '*shell*'     # $true

Reversing -contains

'def' -contains @('abc','def')   # not the intended membership form

Write the collection first:

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

Or use the reversed-operand operator:

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

Expecting a Boolean from collection filtering

This expression returns matching values:

$matches = @('error.log','notes.txt') -like '*.log'

To turn that into an explicit existence test, test the result as a collection:

$matches.Count -gt 0

Alternatively, use -contains when you already know the exact filename you need to find.

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

Using regex syntax with -like

Wildcard patterns and regular expressions are different languages. Patterns for -like use *, ?, and bracket expressions. Use -match when you specifically need a regular expression.

Practical decision checklist

  • Is the left side a collection and the right side an exact item? Use -contains.
  • Is the value text that must fit a wildcard pattern? Use -like.
  • Is the left side an array and do you want matching elements returned? Use collection input with -like.
  • Do you need one Boolean answer for collection membership? Use -contains or -in.
  • Does capitalization matter? Choose the c-prefixed form; use the i-prefixed form to make insensitive behavior explicit.
  • Is the comparison exact scalar equality? Use -eq.
  • Do you need regex features rather than wildcards? Use -match.

Version and documentation scope

Microsoft’s comparison-operator reference for Windows PowerShell 5.1 and its PowerShell 7.x language and tutorial documentation describe this same core distinction. Details around the operators’ surrounding language behavior can vary by installed version, so check the documentation for the PowerShell version running your script when relying on a version-specific edge case.

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.