The Array Contains an Item

Introduction

Searching a list consists of finding whether it contains a certain item or some items. You have many options and many operators.

Checking Whether an Array Contains a Certain Item

To let you find out whether an array contains a certain element, PowerShell provides an operator named -contains. The formula to use it is:

Boolean-result = array -contains value

To use this operator, type the array name followed by -contains and the value to search for. If one of the items in the array is the value to search, the operation produces a True value. If the item is not found in the array, the operation produces a False result. If you want, you can store the reult in a variable and then use that variable as you see it. Here is an example:

$names = @("James", 'Francine', "Paul", "Jeannette", 'Robert', "Annette")

Write-OutPut "============================"
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]

[bool]$result = $names -contains 'Robert'

Write-OutPut "----------------------------"
Write-Host $result
Write-OutPut "============================"

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
============================
Name 1: James
Name 2: Francine
Name 3: Paul
Name 4: Jeannette
Name 5: Robert
Name 6: Annette
----------------------------
True
============================

Here is another version of the program:

$names = @("James", 'Francine', "Paul", "Jeannette", 'Robert', "Annette")

Write-OutPut "============================"

[bool]$result = $names -contains "Paulette"

Write-Host $result
Write-OutPut "============================"

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
============================
False
============================

If you are not planning to use the result of a -contains many times, you can include the operation directly in a conditional statement. Here is an example:

$names = @("James", 'Francine', "Paul", "Jeannette", 'Robert', "Annette")

Write-OutPut "============================"
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]

Write-OutPut "----------------------------"
if($names -contains 'Robert')
{
    Write-Host "The list contains 'Robert'."
}
else
{
    Write-Host "There is no 'Robert' in the list."
}
Write-OutPut "============================"

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
============================
Name 1: James
Name 2: Francine
Name 3: Paul
Name 4: Jeannette
Name 5: Robert
Name 6: Annette
----------------------------
The list contains 'Robert'.
============================

Here is another version of the program:

$names = @("James", 'Francine', "Paul", "Jeannette", 'Robert', "Annette")

Write-OutPut "================================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]

Write-OutPut "---------------------------------"
if($names -contains 'Franc')
{
    Write-Host "The array contains 'Franc'."
}
else
{
    Write-Host "There is no 'Franc' in the array."
}
Write-OutPut "================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
=================================
Name 1: James
Name 2: Francine
Name 3: Paul
Name 4: Jeannette
Name 5: Robert
Name 6: Annette
---------------------------------
There is no 'Franc' in the array.
=================================

Checking Whether an Array Does NOT CONTAIN a Certain Item

To let you find out whether an array doesn't contain a certain member, PowerShell provides an operator named -notcontains. The formula to use it is:

Boolean-result = array -notcontains value

Like its name suggests, this operator functions like the -contains operator, except that it is its opposite. Here is an example of using this operator:

$names = @("Irene", 'Jonathan', "Richard", "Bertha", 'Anthony', "Joy")

Write-OutPut "=================================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]

[bool]$notContain = $names -NotContains "Armando"
Write-OutPut "----------------------------------"
if($notAvailable)
{
    Write-Host "The array does NOT CONTAIN 'Armando'."
}
else
{
    Write-Host "The array contains 'Armando'."
}
Write-OutPut "=================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
==================================
Name 1: Irene
Name 2: Jonathan
Name 3: Richard
Name 4: Bertha
Name 5: Anthony
Name 6: Joy
----------------------------------
The array does NOT contain 'Armando'.
==================================

A Certain Item is IN an Array

Whecking Whether a Certain Item is IN an Array

To let you find out whether a certain item exists in the designated array, PowerShell provides an operator named -in. The formula to use it is:

Boolean-result = value -in array

To use this operator, type the value to search, followed by the -in operator, followed the name of the array. PowerShell would look for that value among the items of the designated array is the value to search, the operation produces a True value. If the item is not found in the array, the operation produces a False result. If you want, you can store the reult in a variable and then use that variable as you see it. Here is an example:

$names = @("James", 'Francine', "Paul", "Jeannette", 'Robert', "Annette")

Write-OutPut "================================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]

[bool]$available = "Paul" -in $names
Write-OutPut "---------------------------------"
if($available -eq $True)
{
    Write-Host "'Paul' is IN the array."
}
else
{
    Write-Host "'Paul' is not in the array."
}
Write-OutPut "================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
=================================
Name 1: James
Name 2: Francine
Name 3: Paul
Name 4: Jeannette
Name 5: Robert
Name 6: Annette
---------------------------------
'Paul' is in the array.
=================================

Here is another version of the program:

$names = @("James", 'Francine', "Paul", "Jeannette", 'Robert', "Annette")

Write-OutPut "================================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]

[bool]$available = "Paula" -in $names
Write-OutPut "---------------------------------"
if($available -eq $True)
{
    Write-Host "'Paula' is IN the array."
}
else
{
    Write-Host "'Paula' is not in the array."
}
Write-OutPut "================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
=================================
Name 1: James
Name 2: Francine
Name 3: Paul
Name 4: Jeannette
Name 5: Robert
Name 6: Annette
---------------------------------
'Paula' is not in the array.
=================================

Whecking Whether a Certain Item is NOT IN an Array

To let you find out whether a certain item doesn't exist in a certain array, PowerShell provides an operator named -notin. The formula to use it is:

Boolean-result = value -notin array

Like its name suggests, this operator functions like the -in operator, except it is its opposite. Here is an example:

$names = @("James", 'Francine', "Paul", "Jeannette", 'Robert', "Annette")

Write-OutPut "================================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]

[bool]$notAvailable = "Paul" -NotIn $names
Write-OutPut "---------------------------------"
if($notAvailable)
{
    Write-Host "'Paul' is NOT IN the array."
}
else
{
    Write-Host "'Paul' is IN the array."
}
Write-OutPut "================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
=================================
Name 1: James
Name 2: Francine
Name 3: Paul
Name 4: Jeannette
Name 5: Robert
Name 6: Annette
---------------------------------
'Paul' is IN the array.
=================================

A Search LIKE a Wild Card

Introduction

The searches we have performed so far used a fixed known value. Another operator you can use to search for an exact string in an array is named -like. The formula to use this operator is:

result-variable = array -like expression

To use this operator, provide an array, followed by -like, and followed by a value to find in an array. This operator primarily works like the -contains operation we saw earlier. That is, provide a value as the expression. PowerShell would look for that string in the indicated array. Here is an example:

$names = @("Henriette", "George", 'Madeleine', "Andy"
           'Marcy', "Jonathan", "Mildred", 'Anthony'
           "Mary", "Joy", "Murielle", 'David')

Write-OutPut "============================================"
Write-Host "Name  1:" $names[0]
Write-Host "Name  2:" $names[1]
Write-Host "Name  3:" $names[2]
Write-Host "Name  4:" $names[3]
Write-Host "Name  5:" $names[4]
Write-Host "Name  6:" $names[5]
Write-Host "Name  7:" $names[6]
Write-Host "Name  8:" $names[7]
Write-Host "Name  9:" $names[8]
Write-Host "Name 10:" $names[9]

$people = $names -LIKE "Madeleine"
Write-OutPut "--------------------------------------------"
Write-Host "Person Found:" $people
Write-OutPut "============================================"

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
============================================
Name  1: Henriette
Name  2: George
Name  3: Madeleine
Name  4: Andy
Name  5: Marcy
Name  6: Jonathan
Name  7: Mildred
Name  8: Anthony
Name  9: Mary
Name 10: Joy
--------------------------------------------
Person Found: Madeleine
============================================

Instead of searching a whole exact string, sometimes, you know only some part of the string you want to search in an array. This the main goal of the -like operator. It allows you to provide an approximate value to search for. That value is presented as the expression in our formula. The expression is created as a pattern, using one or more characters referred to as wild cards. The expression is usually created as a string, included in single or double quotes.

A Starting Pattern

If you have an array, you can ask PowerShell to find all members that start a certain way. The wild character tu use is *. Therefore, for the expression, create a string that starts with * and add one or more characters by which the value should end. PowerShell would then scan the array. Every time it finds such a value, it would put it in a list. Of course, you can store the resulting list in variable, and then use that variable as a list. Here is an example:

$names = @("Henry", "John", 'Andrew',
           "Jonathan", "Margueritte", "Jeannette",
           'Anthony', "James", "Joy")

Write-OutPut "========================================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]
Write-Host "Name 7:" $names[6]
Write-Host "Name 8:" $names[7]
Write-Host "Name 9:" $names[8]

$people = $names -LIKE "J*"
Write-OutPut "-----------------------------------------"
Write-Host "People:" $people
Write-OutPut "========================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
=========================================
Name 1: Henry
Name 2: John
Name 3: Andrew
Name 4: Jonathan
Name 5: Margueritte
Name 6: Jeannette
Name 7: Anthony
Name 8: James
Name 9: Joy
-----------------------------------------
People: John Jonathan Jeannette James Joy
=========================================

Notice that the operation produces an array. As a result, you can use the result as an array and perform any operation that is valid for an array. For example, you can access an item of the result using the [] notation. Here is an example:

$names = @("Henry", "John", 'Andrew',
           "Jonathan", "Margueritte", "Jeannette",
           'Anthony', "James", "Joy")

Write-OutPut "===================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]
Write-Host "Name 7:" $names[6]
Write-Host "Name 8:" $names[7]
Write-Host "Name 9:" $names[8]

$people = $names -like "J*"

Write-OutPut "--------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]
Write-Host "Person 3:" $people[2]
Write-Host "Person 4:" $people[3]
Write-Host "Person 5:" $people[4]
Write-OutPut "===================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
====================
Name 1: Henry
Name 2: John
Name 3: Andrew
Name 4: Jonathan
Name 5: Margueritte
Name 6: Jeannette
Name 7: Anthony
Name 8: James
Name 9: Joy
--------------------
Person 1: John
Person 2: Jonathan
Person 3: Jeannette
Person 4: James
Person 5: Joy
====================

In the above examples, we specied only one character in the expression. In the same way, you can specity as many characters as you want, especially if you want to narrow the search as closely as possible. Consider the following examples:

$names = @("Marc", "Henry", 'Madeleine',
           "Jonathan", "Mildred", 'Anthony'
           "Marie", "Joy", "Murielle")

Write-OutPut "=========================================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]
Write-Host "Name 7:" $names[6]
Write-Host "Name 8:" $names[7]
Write-Host "Name 9:" $names[8]

$people = $names -LIKE "m*"
Write-OutPut "------------------------------------------"
Write-Host "People:" $people
Write-OutPut "------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]
Write-Host "Person 3:" $people[2]
Write-Host "Person 4:" $people[3]
Write-Host "Person 5:" $people[4]

$people = $names -Like "ma*"
Write-OutPut "------------------------------------------"
Write-Host "People:" $people
Write-OutPut "------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]
Write-Host "Person 3:" $people[2]

$people = $names -like "mu*"
Write-OutPut "------------------------------------------"
Write-Host "People:" $people
Write-OutPut "------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-OutPut "=========================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
==========================================
Name 1: Marc
Name 2: Henry
Name 3: Madeleine
Name 4: Jonathan
Name 5: Mildred
Name 6: Anthony
Name 7: Marie
Name 8: Joy
Name 9: Murielle
------------------------------------------
People: Marc Madeleine Mildred Marie Murielle
------------------------------------------
Person 1: Marc
Person 2: Madeleine
Person 3: Mildred
Person 4: Marie
Person 5: Murielle
------------------------------------------
People: Marc Madeleine Marie
------------------------------------------
Person 1: Marc
Person 2: Madeleine
Person 3: Marie
------------------------------------------
People: Murielle
------------------------------------------
Person 1: Murielle
==========================================

An Ending Pattern

Instead of specifying the way an expression should start for a search, you can specify one or more characters by which a string should end. The operations are conducted the samee way we saw in the previous sections except that Powershell would check every string starting from its ending character(s). Here are examples:

$names = @("Henriette", "George", 'Madeleine', "Andy"
           'Marcy', "Jonathan", "Mildred", 'Anthony'
           "Mary", "Joy", "Murielle", 'David')

Write-OutPut "============================================"
Write-Host "Name  1:" $names[0]
Write-Host "Name  2:" $names[1]
Write-Host "Name  3:" $names[2]
Write-Host "Name  4:" $names[3]
Write-Host "Name  5:" $names[4]
Write-Host "Name  6:" $names[5]
Write-Host "Name  7:" $names[6]
Write-Host "Name  8:" $names[7]
Write-Host "Name  9:" $names[8]
Write-Host "Name 10:" $names[9]

$people = $names -LIKE "*y"
Write-OutPut "--------------------------------------------"
Write-Host "People:" $people
Write-OutPut "--------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]
Write-Host "Person 3:" $people[2]
Write-Host "Person 4:" $people[3]
Write-Host "Person 5:" $people[4]

$people = $names -like "*e"
Write-OutPut "--------------------------------------------"
Write-Host "People:" $people
Write-OutPut "--------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]
Write-Host "Person 3:" $people[2]
Write-Host "Person 4:" $people[3]

$people = $names -Like "*d"
Write-OutPut "--------------------------------------------"
Write-Host "People:" $people
Write-OutPut "--------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]

$people = $names -like "*n"
Write-OutPut "--------------------------------------------"
Write-Host "People:" $people
Write-OutPut "--------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-OutPut "============================================"

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
============================================
Name  1: Henriette
Name  2: George
Name  3: Madeleine
Name  4: Andy
Name  5: Marcy
Name  6: Jonathan
Name  7: Mildred
Name  8: Anthony
Name  9: Mary
Name 10: Joy
--------------------------------------------
People: Andy Marcy Anthony Mary Joy
--------------------------------------------
Person 1: Andy
Person 2: Marcy
Person 3: Anthony
Person 4: Mary
Person 5: Joy
--------------------------------------------
People: Henriette George Madeleine Murielle
--------------------------------------------
Person 1: Henriette
Person 2: George
Person 3: Madeleine
Person 4: Murielle
--------------------------------------------
People: Mildred David
--------------------------------------------
Person 1: Mildred
Person 2: David
--------------------------------------------
People: Jonathan
--------------------------------------------
Person 1: Jonathan
============================================

Notice that the operation produces an array. As a result, you can use the result as an array and perform any operation that is valid for an array. For example, you can access an item of the result using the [] notation. Here is an example:

$names = @("Henry", "John", 'Andrew',
           "Jonathan", "Margueritte", "Jeannette",
           'Anthony', "James", "Joy")

Write-OutPut "===================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]
Write-Host "Name 7:" $names[6]
Write-Host "Name 8:" $names[7]
Write-Host "Name 9:" $names[8]

$people = $names -like "J*"

Write-OutPut "--------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]
Write-Host "Person 3:" $people[2]
Write-Host "Person 4:" $people[3]
Write-Host "Person 5:" $people[4]
Write-OutPut "===================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
====================
Name 1: Henry
Name 2: John
Name 3: Andrew
Name 4: Jonathan
Name 5: Margueritte
Name 6: Jeannette
Name 7: Anthony
Name 8: James
Name 9: Joy
--------------------
Person 1: John
Person 2: Jonathan
Person 3: Jeannette
Person 4: James
Person 5: Joy
====================

In the above examples, we specied only one character in the expression. In the same way, you can specity as many characters as you want, especially if you want to narrow the search as closely as possible. Consider the following examples:

$names = @("Marc", "Henry", 'Madeleine',
           "Jonathan", "Mildred", 'Anthony'
           "Marie", "Joy", "Murielle")

Write-OutPut "=========================================="
Write-Host "Name 1:" $names[0]
Write-Host "Name 2:" $names[1]
Write-Host "Name 3:" $names[2]
Write-Host "Name 4:" $names[3]
Write-Host "Name 5:" $names[4]
Write-Host "Name 6:" $names[5]
Write-Host "Name 7:" $names[6]
Write-Host "Name 8:" $names[7]
Write-Host "Name 9:" $names[8]

$people = $names -LIKE "m*"
Write-OutPut "------------------------------------------"
Write-Host "People:" $people
Write-OutPut "------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]
Write-Host "Person 3:" $people[2]
Write-Host "Person 4:" $people[3]
Write-Host "Person 5:" $people[4]

$people = $names -Like "ma*"
Write-OutPut "------------------------------------------"
Write-Host "People:" $people
Write-OutPut "------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-Host "Person 2:" $people[1]
Write-Host "Person 3:" $people[2]

$people = $names -like "mu*"
Write-OutPut "------------------------------------------"
Write-Host "People:" $people
Write-OutPut "------------------------------------------"
Write-Host "Person 1:" $people[0]
Write-OutPut "=========================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
==========================================
Name 1: Marc
Name 2: Henry
Name 3: Madeleine
Name 4: Jonathan
Name 5: Mildred
Name 6: Anthony
Name 7: Marie
Name 8: Joy
Name 9: Murielle
------------------------------------------
People: Marc Madeleine Mildred Marie Murielle
------------------------------------------
Person 1: Marc
Person 2: Madeleine
Person 3: Mildred
Person 4: Marie
Person 5: Murielle
------------------------------------------
People: Marc Madeleine Marie
------------------------------------------
Person 1: Marc
Person 2: Madeleine
Person 3: Marie
------------------------------------------
People: Murielle
------------------------------------------
Person 1: Murielle
==========================================

A Search NOT LIKE a Known Value

Consider the following code:

$names = @("Henriette", "George", 'Madeleine', "Andy"
           'Marcy', "Jonathan", "Mildred", 'Anthony'
           "Mary", "Joy", "Murielle", 'David')

Write-OutPut "=========================================================================================="
Write-Host "Person  1:    " $names[0]
Write-Host "Person  2:    " $names[1]
Write-Host "Person  3:    " $names[2]
Write-Host "Person  4:    " $names[3]
Write-Host "Person  5:    " $names[4]
Write-Host "Person  6:    " $names[5]
Write-Host "Person  7:    " $names[6]
Write-Host "Person  8:    " $names[7]
Write-Host "Person  9:    " $names[8]
Write-Host "Person 10:    " $names[9]
Write-Host "Person 11:    " $names[10]
Write-Host "Person 12:    " $names[11]
Write-OutPut "------------------------------------------------------------------------------------------"

$people = $names -LIKE "Madeleine"

Write-Host "Person Found: " $people

Write-OutPut "=========================================================================================="

$people = $names -Like "M*"

Write-Host "People 'M*':  " $people

Write-OutPut "------------------------------------------------------------------------------------------"
Write-Host "Person 1:     " $people[0]
Write-Host "Person 2:     " $people[1]
Write-Host "Person 3:     " $people[2]
Write-Host "Person 4:     " $people[3]
Write-Host "Person 5:     " $people[4]
Write-OutPut "=========================================================================================="

$people = $names -Like "Ma*"

Write-Host "People 'Ma*': " $people

Write-OutPut "------------------------------------------------------------------------------------------"
Write-Host "Person 1:     " $people[0]
Write-Host "Person 2:     " $people[1]
Write-Host "Person 3:     " $people[2]
Write-OutPut "=========================================================================================="

$people = $names -Like "Mar*"

Write-Host "People 'Mar*':" $people

Write-OutPut "------------------------------------------------------------------------------------------"
Write-Host "Person 1:     " $people[0]
Write-Host "Person 2:     " $people[1]
Write-OutPut "=========================================================================================="

$people = $names -like "jo*"

Write-Host "People 'Jo*': " $people

Write-OutPut "------------------------------------------------------------------------------------------"
Write-Host "Person 1:     " $people[0]
Write-Host "Person 2:     " $people[1]
Write-OutPut "=========================================================================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
==========================================================================================
Person  1:     Henriette
Person  2:     George
Person  3:     Madeleine
Person  4:     Andy
Person  5:     Marcy
Person  6:     Jonathan
Person  7:     Mildred
Person  8:     Anthony
Person  9:     Mary
Person 10:     Joy
Person 11:     Murielle
Person 12:     David
------------------------------------------------------------------------------------------
Person Found:  Madeleine
==========================================================================================
People 'M*':   Madeleine Marcy Mildred Mary Murielle
------------------------------------------------------------------------------------------
Person 1:      Madeleine
Person 2:      Marcy
Person 3:      Mildred
Person 4:      Mary
Person 5:      Murielle
==========================================================================================
People 'Ma*':  Madeleine Marcy Mary
------------------------------------------------------------------------------------------
Person 1:      Madeleine
Person 2:      Marcy
Person 3:      Mary
==========================================================================================
People 'Mar*': Marcy Mary
------------------------------------------------------------------------------------------
Person 1:      Marcy
Person 2:      Mary
==========================================================================================
People 'Jo*':  Jonathan Joy
------------------------------------------------------------------------------------------
Person 1:      Jonathan
Person 2:      Joy
==========================================================================================

In the previous -like expressions, we provided either an exact string to search or a section of the string we are looking for. To help you find a string that is not what you provide, PowerShell provides an operator named -notlike. The formula to use this operator is the same as that of the -like operator:

result-variable = array -notlike expression

This time, PowerShell would consider the indicated array but produce a list of items are not like the provided expressions. Here are examples:

$names = @("Henriette", "George", 'Madeleine', "Andy"
           'Marcy', "Jonathan", "Mildred", 'Anthony'
           "Mary", "Joy", "Murielle", 'David')

Write-OutPut "==================================================================================================="
Write-Host "Person  1:    " $names[0]
Write-Host "Person  2:    " $names[1]
Write-Host "Person  3:    " $names[2]
Write-Host "Person  4:    " $names[3]
Write-Host "Person  5:    " $names[4]
Write-Host "Person  6:    " $names[5]
Write-Host "Person  7:    " $names[6]
Write-Host "Person  8:    " $names[7]
Write-Host "Person  9:    " $names[8]
Write-Host "Person 10:    " $names[9]
Write-Host "Person 11:    " $names[10]
Write-Host "Person 12:    " $names[11]
Write-OutPut "---------------------------------------------------------------------------------------------------"

$people = $names -NOTLIKE "Madeleine"

Write-Host "People NOT Madeleine:" $people

Write-OutPut "---------------------------------------------------------------------------------------------------"
Write-Host "Person  1:    " $people[0]
Write-Host "Person  2:    " $people[1]
Write-Host "Person  3:    " $people[2]
Write-Host "Person  4:    " $people[3]
Write-Host "Person  5:    " $people[4]
Write-Host "Person  6:    " $people[5]
Write-Host "Person  7:    " $people[6]
Write-Host "Person  8:    " $people[7]
Write-Host "Person  9:    " $people[8]
Write-Host "Person 10:    " $people[9]
Write-Host "Person 11:    " $people[10]
Write-OutPut "==================================================================================================="

$people = $names -NotLike "M*"

Write-Host "People NOT 'M*': " $people

Write-OutPut "---------------------------------------------------------------------------------------------------"
Write-Host "Person 1:     " $people[0]
Write-Host "Person 2:     " $people[1]
Write-Host "Person 3:     " $people[2]
Write-Host "Person 4:     " $people[3]
Write-Host "Person 5:     " $people[4]
Write-Host "Person 6:     " $people[5]
Write-Host "Person 7:     " $people[6]
Write-OutPut "==================================================================================================="

$people = $names -NotLike "Ma*"

Write-Host "People NOT 'Ma*': " $people

Write-OutPut "---------------------------------------------------------------------------------------------------"
Write-Host "Person 1:     " $people[0]
Write-Host "Person 2:     " $people[1]
Write-Host "Person 3:     " $people[2]
Write-Host "Person 4:     " $people[3]
Write-Host "Person 5:     " $people[4]
Write-Host "Person 6:     " $people[5]
Write-Host "Person 7:     " $people[6]
Write-Host "Person 8:     " $people[7]
Write-Host "Person 9:     " $people[8]
Write-OutPut "==================================================================================================="

$people = $names -notlike "Mar*"

Write-Host "People NOT 'Mar*':" $people

Write-OutPut "---------------------------------------------------------------------------------------------------"
Write-Host "Person 1:     " $people[0]
Write-Host "Person 2:     " $people[1]
Write-Host "Person 3:     " $people[2]
Write-Host "Person 4:     " $people[3]
Write-Host "Person 5:     " $people[4]
Write-Host "Person 6:     " $people[5]
Write-Host "Person 7:     " $people[6]
Write-Host "Person 8:     " $people[7]
Write-Host "Person 9:     " $people[8]
Write-Host "Person 10:    " $people[9]
Write-OutPut "==================================================================================================="

$people = $names -notlike "jo*"

Write-Host "People NOT 'Jo*': " $people

Write-OutPut "---------------------------------------------------------------------------------------------------"
Write-Host "Person 1:     " $people[0]
Write-Host "Person 2:     " $people[1]
Write-Host "Person 3:     " $people[2]
Write-Host "Person 4:     " $people[3]
Write-Host "Person 5:     " $people[4]
Write-Host "Person 6:     " $people[5]
Write-Host "Person 7:     " $people[6]
Write-Host "Person 8:     " $people[7]
Write-Host "Person 9:     " $people[8]
Write-Host "Person 10:    " $people[9]
Write-OutPut "==================================================================================================="

This would produce:

PS C:\Arrays> C:\Arrays\Exercise1.ps1
===================================================================================================
Person  1:     Henriette
Person  2:     George
Person  3:     Madeleine
Person  4:     Andy
Person  5:     Marcy
Person  6:     Jonathan
Person  7:     Mildred
Person  8:     Anthony
Person  9:     Mary
Person 10:     Joy
Person 11:     Murielle
Person 12:     David
---------------------------------------------------------------------------------------------------
People NOT Madeleine: Henriette George Andy Marcy Jonathan Mildred Anthony Mary Joy Murielle David
---------------------------------------------------------------------------------------------------
Person  1:     Henriette
Person  2:     George
Person  3:     Andy
Person  4:     Marcy
Person  5:     Jonathan
Person  6:     Mildred
Person  7:     Anthony
Person  8:     Mary
Person  9:     Joy
Person 10:     Murielle
Person 11:     David
===================================================================================================
People NOT 'M*':  Henriette George Andy Jonathan Anthony Joy David
---------------------------------------------------------------------------------------------------
Person 1:      Henriette
Person 2:      George
Person 3:      Andy
Person 4:      Jonathan
Person 5:      Anthony
Person 6:      Joy
Person 7:      David
===================================================================================================
People NOT 'Ma*':  Henriette George Andy Jonathan Mildred Anthony Joy Murielle David
---------------------------------------------------------------------------------------------------
Person 1:      Henriette
Person 2:      George
Person 3:      Andy
Person 4:      Jonathan
Person 5:      Mildred
Person 6:      Anthony
Person 7:      Joy
Person 8:      Murielle
Person 9:      David
===================================================================================================
People NOT 'Mar*': Henriette George Madeleine Andy Jonathan Mildred Anthony Joy Murielle David
---------------------------------------------------------------------------------------------------
Person 1:      Henriette
Person 2:      George
Person 3:      Madeleine
Person 4:      Andy
Person 5:      Jonathan
Person 6:      Mildred
Person 7:      Anthony
Person 8:      Joy
Person 9:      Murielle
Person 10:     David
===================================================================================================
People NOT 'Jo*':  Henriette George Madeleine Andy Marcy Mildred Anthony Mary Murielle David
---------------------------------------------------------------------------------------------------
Person 1:      Henriette
Person 2:      George
Person 3:      Madeleine
Person 4:      Andy
Person 5:      Marcy
Person 6:      Mildred
Person 7:      Anthony
Person 8:      Mary
Person 9:      Murielle
Person 10:     David
===================================================================================================


Previous Copyright © 2001-2025, FunctionX Wednesday 12 February 2025, 16:36 Next