In this article, we will look at how to use the PowerShell Substring command to find a specific string inside another text file. We’ll provide instructions for both Windows and Linux users so that anyone can follow along!
Finding a technique to discover a specific fragment of text inside a string, known as a substring, is a regular occurrence in an administrator’s world. Finding a substring is a breeze using PowerShell.
Expanding Strings and Learning the String Format are two related topics.
Strings and PowerShell
A string in PowerShell is a collection of characters contained in single or double quotes. Strings such as “foo” and “bar” are quite prevalent.
Back to the Basics: PowerShell Strings
Let’s imagine you have a string specified in a variable and you just need to locate a portion of it. As an example, suppose you had a string containing the address 1234 4th 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 need. You may utilize the PowerShell substring() technique in this case.
Using the SubString Method in PowerShell
The Substring() function in PowerShell may be used to locate a string inside a string. Every string object in PowerShell has this function.
Perhaps you have a string like The swift brown fox leapt over the fence on your hands. The first five characters are what you’re looking for. You might use the Substring() function to do this:
‘The swift brown fox leapt over the fence,’ $string = ‘ $string.Substring(0,5)
The leftmost character’s location is the first input to send to the Substring() function. The leftmost character in this example is T. The second argument is the character position that is farthest to the right. The character in this example is q.
All of the characters between them are returned by the Substring() function.
Here’s an example from the actual world:
Let’s imagine we have a product code that looks like this: XXXXVVVV-MM-DD-YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY This coding format is used by all products, and it is never changed. Let’s pretend the inventory management software that developed this code didn’t have a date created column in the database.
However, the program always puts this date in the product code as MM-DD-YYYYY, so we can deduce it from the product code. We need this date for our products so that we can generate a nice but pointless report for management that shows when each product was added to the database. Here’s an excellent approach to go about it.
$product code = ‘ABCD1234-11-12-2013’; $product code = ‘ABCD1234-11-12-2013’; $product code = [DateTime] $date created $product code.SubString($product code.Length-10)
For the intrinsic string object, we’re using the PowerShell SubString() function and supplying the number for the first character we wish to locate as the input.
Because we don’t provide a stopping character, the program thinks that we want all characters to reach the conclusion of the string. To acquire the number in the first place, we need to retrieve the length of the complete product code and remove 10 from it. This signifies that we want to start at the tenth character from the right of the string and go all the way to the right.
Although the [DateTime] type does not always correspond to the post’s substring subject, I felt it would work well with the particular case.
After getting our string of 11-12-2013, we type cast it to a [DateTime] object, which turns a stupid string into a useful object. We can conduct all of the sophisticated date arithmetic management needs and produce that beautiful bar chart report from here!
Using the Length Property to Find a Substring Dynamically
You were statically determining the start and end locations of the characters within the string in the preceding example. But what if you don’t know where you were last?
It’s possible that you’ll need to locate the substring made up of the final four characters. The set of characters from the fourth to the final place must be found all the way to the finish. It doesn’t matter how long the string is that you’re looking for.
Instead of specifying the end position as a positive integer counting from the left, we may dynamically determine the end position by subtracting a particular number of characters from the string’s length.
Maybe you want to retrieve the final four characters using the string $product code = ‘ABCD1234-11-12-2013’. Instead of doing anything like this, consider the following:
PS> $product_code = ‘ABCD1234-11-12-2013’ PS> $product_code.SubString(0,4) ABCD
$product code may be used in lieu of 0. It will return the final four characters if you use length – 4 and don’t use the end position at all, as demonstrated below.
PS> $product_code = ‘ABCD1234-11-12-2013’ PS> $product_code.SubString($product_code.Length-4) 2013
The PowerShell substring technique will always default to the final character position if the end position is not specified.
You may dynamically choose substrings by subtracting from the length property of the string, which represents the total number of characters in the string.
Frequently Asked Questions
How do I find a string in a string in PowerShell?
A: To find a string in another string, use the following syntax.
How do you check if a string contains a substring in PowerShell?
A: The string.Contains() function will tell you if a substring exists in the provided string.
How do I match a string in PowerShell?
A: The PowerShell string matching cmdlet is called $matches.