Use Powershell To Check If String Contains A Word

      No Comments on Use Powershell To Check If String Contains A Word

Strings tend to be difficult to handle when attempting to locate or modify specific words and characters. Powershell provides quite a few tools to search for special characters, replace words, remove spaces and much more.
Here is a quick example to test and find a word within a string, assuming we already know what the word is. This is more of a Test because the result returned is $True or $False and very useful for an IF statement.

$myString = "String Numbers:  13,25,46 6789 and chars: @ ! / "

$myString -match '6789'    ## $True 
$myString -match 'mbers:'  ## $True 
$myString -match 'cheese'  ## $False 
$myString -match ' '       ## $True 

String Contains Test

We can also use the Contains method to search for the presence of the string.

$myString = "Some String with Numbers:  13,25,46 6789 and characters: @ ! / "  

$myString.Contains('Num')    ## $True
$myString.Contains('25')     ## $True
$myString.Contains('pizza')  ## $False 

Substring Indexof

The ‘IndexOf’ is used to identify the substring character location where it begins. It returns the numeric location starting from the left and counting from 0. We can then use this number as a starting point to collect the next X characters.

$myString = "mixing 13 numbers 25 and 6g7d1 and characters"  

$myString.IndexOf('numbers')  ## 10

Split String Into An Array

The string “Split” function with search the string for a provided ‘string’ and return all of the elements between in an array. This is very useful when trying to break up a folder or file path and work with the components. You end up with the Drive, folders and file name but not the ‘\’ character.

$myPath = "C:\somefolder\anotherfolder\myfile.txt"

$array = $myPath.Split('\')
$array[0]  ## C:
$array[2]  ## anotherfolder

## Example using space or commas
$myString = "Some String with Numbers:  13,25,46 6789 and characters: @ ! / "

$spacearray = $myString.Split(' ')  ## spaces
$commaarray = $myString.Split(',')  ## commas

Use -Contains to Find Word in Array

With the string elements saved to an array we can use the Array object “-Contains” function to find the word or string. Notice that partial strings do not return a $True.

$myPath = "C:\somefolder\anotherfolder\myfile.txt"
$array = $myPath.Split('\')

$array -Contains 'anotherfolder' ## $True
$array -Contains 'some'          ## $False
$array -Contains '.txt'          ## $False
$array -Contains 'myfile.txt'    ## $True

Regular Expression

Another useful array function is using the Regular Expression built into Powershell. Try these out as they are (db tales com) different then returning a True or False. With “\D” we get a ‘new line’ in the returned values until it finds a Digit.
With “\d” we get the opposite, characters and words returned but not the numbers.
Also we can use the LIKE operator to search for a specific digit.

$myString = "mixing 13 numbers 25 and 6g7d1 and characters"  

$myString -split "\D"
$myString -split "\D" -like '*25*'

$myString -split "\d"
$myString -split "\d" -like '*char*'