How to Find a string inside a string -PowerShell Substring

The PowerShell Substring : Finding a string inside a string

There are many occassions where Administrators need to figure out a way to find a certain snippet of text inside a string which is called the substring. PowerShell makes finding a substring extremely easy.

How are Strings handled in PowerShell?

A string in PowerShell is a collection of characters contained in single or double quotes. Strings examples  are  say “Adam” and “Bharat”.
Let’s imagine you have a string defined in a variable and you only need to find a portion of it. As an example, suppose you had a string with the address 142 wardour st. You want to be able to take out the phone number and know that the first four characters will always be the number you require. You can utilize the PowerShell substring() technique in this case.

Using the SubString Method in PowerShell

The Substring() method in PowerShell can be used to find a string within a string. Every string object in PowerShell has this method.

 

Perhaps you have a string like  above “142 wardour street”.  The first ten characters are what you’re looking for. You might use the Substring() function to accomplish this:

$string = '142 wardour street'
$string.Substring(0,10)

The leftmost character’s location is the first input to send to the Substring() function. The leftmost character in this example is 1. The second argument is the character position that is furthest to the right. The character in this example is u.
All of the characters between them are returned by the Substring() function.

Will return 142 wardou

In real world many programs puts the date in the various usefull descriptions like MM-DD-YYYYY, so we can deduce it from the them. We need this date for our list so that we can generate a nice report for management that shows it by Date

Now for utilizing the PowerShell SubString() technique

$Description = 'jdfdhf-11-02-2020'

$date_created = [DateTime]$Description.SubString($Description.Length-10)

 

After getting our string of 11-12-2013, we type cast it to a [DateTime] object, which turns a string into a useful object. We can conduct all of the fancy date arithmetic functions and generate that fancy bar chart report etc..

How to Dynamically use Length Property to Find a Substring?

You were statically determining the start and end positions of the characters inside the string in the preceding example. But what if you don’t know where you were last?

PS> $Description = 'dfdfdf-11-02-2020'
PS> $Description.SubString(0,4)
dfdf

Now we can replace 0 with $Description.Length - 10 and not even use the end position at all and it will return the last ten characters as shown below.


PS> $Description = 'dfdfdf-11-02-2020'
PS> $Description.SubString($Description.Length-10)
11-02-2020

Another example using split:
("OU=test1,OU=test2,DC=one,DC=test" -split ",")

OU=test1
OU=test2
DC=one
DC=test

PS C:\> ("OU=MTL1,OU=CORP,DC=FX,DC=LAB" -split ',')[0]

OU=test1
PS C:\> ("OU=MTL1,OU=CORP,DC=FX,DC=LAB" -split ',')[0].substring(3)

OU=tes

Using Regex for split

("OU=test1,OU=test2,DC=one,DC=test" -split ',*..=')[1]

OU=test1

Leave a Reply

Your email address will not be published.