After the extraction of the file name and the value to check it will then import the CSV file which has that filename and check for the extracted value in it. If the CSV file contains the value it will append the values from the specified fields in the CSV file to the specified text file. Let us talk about our first case, checking if given string variable contains a number. Look at the below example. The variable $myvar has a value “123”. Though it is a numeric number, PowerShell sees it as a String. Summary: Learn how to check a string to see if it contains another string. How can I use Windows PowerShell to check a string to see if it contains another string? Use the –Match operator.
At some point during your PowerShelling career you will need to test if “something” is found (or not found) within a certain object. I find that I am usually faced with this situation when I am testing if a string “contains” a value or not.
At this point, I am always confused as to which comparison operator to use. From a logical language perspective I always feel like -contains
is the way to go, but then I remember that might not be the correct choice.
To put this issue to bed once and for all (at least for myself), here is a summary of when to use -like
and when to use -contains
.
PowerShell -like Comparison Operator
Here is how Microsoft TechNet describes the use for -like
:
-Like
Description:
Match using the wildcard character (*).
Example:
PS C:> “Windows PowerShell” -like “*shell”
True
PS C:> “Windows PowerShell”, “Server” -like “*shell”
Windows PowerShell
Here is my version:
-like
is used to compare or find if a string exists within another string. -like
allows you to use the wildcard character (*
) so you can search anywhere within the string.
So back to my initial requirement of determining if a string contains a particular value or not, we would use the -like
operator. See some examples below:
In example 1, the result is false because there are no wildcards, therefore its checking to see if the string matches exactly.
In example 2 we added the wildcard (*
) at the end of the search term. This essentially means check if xyz
exists at the beginning of the string and then ignore the rest. In this case it doesn’t, hence the result is false.
In example 3 we are doing the same as example 2 but searching for abc*
and therefore returns true because abc
is found at the beginning of the string. The rest is then ignored due to the wildcard.
Example 4 is essentially a reverse of example 3. We are searching for xyz
at the end of the string and don’t care about anything in front of it (hence the wildcard in the front of the search term).
Example 5 shows searching for something in the middle of the string by using wildcards on either side of the search term.
PowerShell -contains Comparison Operator
Here is how Microsoft TechNet describes the use for -contains
:
Description:
Containment operator. Tells whether a collection of reference values includes a single test value. Always returns a Boolean value. Returns TRUE only when the test value exactly matches at least one of the reference values.
When the test value is a collection, the Contains operator uses reference equality. It returns TRUE only when one of the reference values is the same instance of the test value object.
Syntax:
-Contains
Examples:
PS C:> “abc”, “def” -Contains “def”
True
PS C:> “Windows”, “PowerShell” -Contains “Shell”
False #Not an exact match
# Does the list of computers in $domainServers
# include $thisComputer?
# ——————————————
PS C:> $domainServers -Contains $thisComputer
True
PS C:> “abc”, “def”, “ghi” -Contains “abc”, “def”
False
PS C:> $a = “abc”, “def”
PS C:> “abc”, “def”, “ghi” -Contains $a
False
PS C:> $a, “ghi” -Contains $a
True
Now here is my version….
The -contains
comparison operator is used to determine if a collection of objects (e.g. an array) contains a particular object or not. Galactic civilizations 3 star trek mods. The evaluation is completed against the entire object and not any single object property. In other words, all of the object’s properties are taken into consideration.
That might be a little confusing, but looking at some examples might clear things ups somewhat:
The first example is an easy one, an array with each object being a string and therefore makes the comparison really easy. The example above is searching for the array object (array element) of xyz
. As there is an array element that matches xyz
it will return true.
Now you would expect that the example above would return true, because every Windows system has a W32Time
service, but it doesn’t… why? Well because we are attempting to find an object based on a single property, name
. The -contains
operator doesn’t work like this however. It compares every single property of the object and if all of them are the same then it returns True, else it will return false.
In the example above, we are essentially comparing a string object with a System.ServiceProcess.ServiceController
object, which isn’t going to work.
Powershell If String Contains Text
In order to find a service with the name
property we would need to do:
Although this works, it might not be the best result because W32Time
might be the value of a property within another service and hence you might get some false positives.
To remove the risk of false positives, I would personally do something like:
Enumerating all services in the collection and determining if each of their name
properties equal the search term is a very accurate way to determine if the particular service exists or not and will eliminate all false positives. More code, but a better solution.
Not Searches?
As a quick side-note, what happens if you want determine if something does not contain a particular search term?
Well in that case just use -NotLike
or -NotContains
in the same fashion and in the same situations as you would use -like
and -contains
.
More Info on Comparison Operators
If you want to learn more about all the comparison operators, then check out the TechNet Comparison Operators page.
Powershell If String Contains Substring
Well I hope that helps make things a little clearer on when to use -Like
and when to use -Contains
.
Powershell String Contains Word
Let me know your thoughts below…
Thanks
Luca