Coding Ref

How to use -Contains in PowerShell

How to use -Contains in PowerShell

To check if a string contains a specific value in PowerShell, you can use the -Contains operator. This operator is used to determine whether a string contains a given value or not.

Here's an example of using the -Contains operator:

$string = "Hello World"
if ($string -Contains "World") {
Write-Output "The string contains the value 'World'"
} else {
Write-Output "The string does not contain the value 'World'"
}

In this example, the -Contains operator is used to check if the string $string contains the value World. If it does, the code will output The string contains the value 'World'. If it does not, it will output The string does not contain the value 'World'.

Is contain case sensitive?

By default, the -Contains operator in PowerShell is case-sensitive. This means that it will only return $true if the string or array contains the exact value that you are searching for, including the correct case.

Here's an example of how the -Contains operator is case-sensitive:

$string = "Hello World"
if ($string -Contains "world") {
Write-Output "The string contains the value 'world'"
} else {
Write-Output "The string does not contain the value 'world'"
}

In this example, the -Contains operator is used to check if the string $string contains the value world. Since the string Hello World does not contain the value world (with a lowercase w), the code will output The string does not contain the value 'world'.

If you want to perform a case-insensitive search with the -Contains operator, you can use the -Casesensitive parameter and set it to $false. Here's an example:

$string = "Hello World"
if ($string -Contains "world" -Casesensitive $false) {
Write-Output "The string contains the value 'world'"
} else {
Write-Output "The string does not contain the value 'world'"
}

In this example, the -Contains operator is used to check if the string $string contains the value world. Since the -Casesensitive parameter is set to $false, the -Contains operator will perform a case-insensitive search and will return $true if the string contains the value world, regardless of the case of the letters. In this case, the code will output The string contains the value 'world'.

How to check if an array contains a value

You can use the -Contains operator with an array of values. For example:

$array = "Hello", "World", "Foo", "Bar"
if ($array -Contains "World") {
Write-Output "The array contains the value 'World'"
} else {
Write-Output "The array does not contain the value 'World'"
}

In this example, the -Contains operator is used to check if the array $array contains the value World.

If it does, the code will output The array contains the value 'World'. If it does not, it will output The array does not contain the value 'World'.

Conclusion

To check if a string contains a specific value in PowerShell, you can use the -Contains operator. This operator is used to determine whether a string contains a given value or not.

You'll also like

Related tutorials curated for you

    How to pause in PowerShell

    Everything about certificates in PowerShell

    Not equal to in PowerShell

    How to use -filter in Get-ChildItem in PowerShell

    Do Until in PowerShell

    How to order a hashtable in PowerShell

    How to wait in PowerShell

    What is read-host in PowerShell?

    What is PowerShell ExpandProperty?

    What is Echo in PowerShell?

    How to write multiple-line comments in PowerShell

    How to use ErrorAction in PowerShell