Windows PowerShell -Contains
When it comes filtering, or finding data we are spoilt for choice with -Match, -Like and -Contains. While there is overlap, each conditional operator has a distinctive role in PowerShell scripting.
-Contains is best for seeking an exact value. If I want a command to return either True or False, I start with -Contains, whereas, if I want a list of results then I try -Match or -Like.
Topics for PowerShell’s -Contains Conditional Operator
- Example 1: PowerShell -Contains (Basic)
- Example 2: Seeking a Value in a Collection
- Example 3: PowerShell -Contains Spreadsheet
- Example 4: PowerShell -CContains
- Example 5: PowerShell -NotContains
- PowerShell -Match
- PowerShell -Like
PowerShell uses singular nouns; thus “contains” is a verb, and not a plural noun. A feature of -Contains is that usually returns “True” or “False. If you are looking for a command to return a list of values, then employ -Match or -Like.
# PowerShell -Contains Operator Clear-Host $ArraySimple =@("House","Flat","Bungalow") $ArraySimple -Contains "Flat" # Result PS> True
Example 1b: PowerShell’s -Contains is Very Strict
-Contains can be frustrating because it’s so picky. After a while you realize that this just a design feature, if you don’t like it, try -Match.
# PowerShell -Contains Operator Clear-Host $ArraySimple =@("House","Flats","Bungalow") $ArraySimple -Contains "Flat" # Result PS> False
Note 1: -Contains interprets “Flats” and “Flat” as different, thus returns False; it does not matter that Flat is a subset of Flats.
Challenge: Substitute -Match for -Contains.
Guy Recommends: Free WMI Monitor for PowerShell (FREE TOOL)
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor for PowerShell so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guesswork out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
-Contains would be my choice of conditional operators for situations where I wanted to test for one particular item in a collection, array or a hashtable.
# PowerShell -Contains $Collection = "Peter Smith", "Paula Harris", "Joan Evans" $Collection -Contains "Paula" # Result PS> False
Note 2: If you coded: $Collection -Contains “Paula Harris”
The result would be: True. You need an exact match of the full item.
Note 3: $Collection -Contains “Paula*” does not help. Wildcards are next to useless with -Contains. Better: try -Match Paula, or -Like Paula*
Let us assume we wish to search in a file called links.csv. Furthermore, we can use Import-Csv to read the data so that we can test values with -Contains.
My spreadsheet is called links.csv
I stored the file in D:\PowerShell
The column name is “Custom channel”
See screenshot to the right.
# PowerShell -Contains Operator Clear-Host $File = "D:\Powershell\links.csv" $Check = (Import-Csv $File)."Custom channel" $Check -Contains "linktop" # Result PS> True
Guy Recommends: Network Performance Monitor (FREE TRIAL)
SolarWinds Network Performance Monitor (NPM) will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM on a 30-day free trial.
As with PowerShell’s other conditional operators, you can force them to be case sensitivity by preceding the command with a ‘C’; CContains is not a typo!
# PowerShell -Contains Operator Clear-Host $File = "D:\Powershell\links.csv" $Check = (Import-Csv $File)."Custom channel" $Check -CContains "linktop" # Result PS> False
Note 4: The point is that in the spreadsheet the value is clearly ‘LinkTop’, when we force case-sensitivity with CContains, this is not the same as ‘linktop’, hence a False result.
The negative -NotContains is not as useful as -NotMatch. However, from what we have already learned the syntax is predictable.
# PowerShell -Contains Operator Clear-Host $File = "D:\Powershell\links.csv" $Check = (Import-Csv $File)."Custom channel" $Check -NotContains "link" # Result PS> True
Note 5: Remember that with -Contains, and by extension -NotContains, the match has to be exact. There is no ‘Custom channel’ with the name of precisely ‘link’.
Guy Recommends: SolarWinds Engineer’s Toolset (FREE TRIAL)
This Engineer’s Toolset provides a comprehensive console of 50 utilities for troubleshooting computer problems. Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.
There are so many good gadgets; it’s like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools. Try the SolarWinds Engineer’s Toolset on a 14-day free trial now!
# For even more information about PowerShell Operators try: Get-Help about_Operators
Here is the List of the Types of PowerShell’s Operators
- Arithmetic (+ * – /)
- Assignment (= also -= +=)
- Comparison ( -Match and -Like; also: -eq -gt)
- Logical ( -And -Not)
- Redirectional ( > )
- Split and Join ( -split)
- Type (-Is -Isnot)
- Unary ($i++)
Summary of PowerShell’s -Contains Conditional Operator
When you seek an exact value, then -Contains would be my first choice of conditional operator. -Match or -Like are better suited to scenarios where you only need a partial match, or you need a list of items.
See more Windows PowerShell flow control examples
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.