Powershell Check If File Contains String

Posted on  by 

  1. Powershell Check If File Path Contains String
  2. Powershell Check If File Contains Strings
  3. Powershell String Contains Word
  4. Powershell If File Contains String Do Action

I have always wondered about the PowerShell -contains clause. Inspecting an object for something is very important in many IT processes. The ‘natural’ name of this doesn’t seem to fit its use and common sense on how to think about using this.

In the first two examples, the script checks the string ends to see if it ends in a backslash. In the last two examples, the script check the string to see if it starts with one. The regex pattern being matched for the first two is $. What’s that mean? I have a text file which has directory locations, one directory location per line. What I am trying to do is, loop a set of directories and check if their FullName exists in the said text file. I tried couple of approaches but I don't seem to be getting the expected result. PowerShell -contains -like Don Jones Dec 20, 2011 When folks are exploring the operators available in PowerShell ( help aboutcomparisonoperators ), they'll often run across two similar-seeming, but drastically different, operators - and get confused by them. Up to PowerShell version 6.1.2, when the IsValid and PathType switches are specified together, the Test-Path cmdlet ignores the PathType switch and only validates the syntactic path without validating the path type.

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

Check if text file contains string powershell

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).

Powershell Check If File Path Contains String

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.

Powershell Check If File Contains String

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. 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.

Powershell Check If File Contains Strings

In the example above, we are essentially comparing a string object with a System.ServiceProcess.ServiceController object, which isn’t going to work.

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.

Check

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.

Well I hope that helps make things a little clearer on when to use -Like and when to use -Contains.

Let me know your thoughts below…

Powershell String Contains Word

Thanks
Luca

TagsAutomation, PowerShell, Scripting

You want to be able to check and allow only date and time strings that are in a specific date time format pattern. For example, you get a string and want to check if it is in a sortable date and time pattern.

Let’s find first how a sortable date and time string looks like. Note that format string is culture dependent:

Contains

Instead of using, or having to remember that long pattern, the .NET Framework offers us a composite format string
that is an equivalent of the above, kind of a shortcut pattern, the “s” standard format string. We can format a date and time object to a sortable pattern with the Get-Date cmdlet.

Now, given a date and time string, how you check it complies to a specific pattern?

Powershell If File Contains String Do Action

The DateTime .NET structure has a static method we can use to parse strings and return DateTime objects, the ParseExact method. It converts the specified string representation of a date and time to its DateTime equivalent. If the format of the string does not match a specified format exactly an exception is thrown.

The method accepts three arguments:

Check
  1. s – A string that contains a date and time to convert.
  2. format – A format specifier that defines the required format of ‘s’.
  3. provider – An object that supplies culture-specific format information about ‘s’.

Wrapping it all into a function so we can reuse it any time we need to.

The function returns True/False if a given string is in the correct format. Add the PassThru switch to get back the parsed date and time object if the pattern was successfully parsed.

Coments are closed