Home ยป How to Extract Text From String in PowerShell

How to Extract Text From String in PowerShell

PowerShell offers various methods to extract text from a string.

Method 1: Extract text from a string using the Substring() method

"Hello, World".Substring(startIndex,length)

This example will extract only the word from the string for the given start index and length.

Method 2: Extract text between brackets in a string

[regex]::Matches($original_string, $pattern).Value

This example uses the regex expression to match the given pattern $pattern in the $original_string and returns a string between brackets.

Method 3: Get text between parentheses

[regex]::Matches($original_string, '(?<=\()(.*?)(?=\))').Value

This example uses the regex expression to get a string between parentheses from the input string $original_string.

Method 4: Get text between quotes

$matches = $original_string | Select-String -Pattern $pattern -AllMatches

This example uses the Select-String command with the -AllMatches parameter to find all matches of the pattern in the input string $original_string.

Method 5: Get text between HTML tags

$matches = $original_string | Select-String -Pattern $pattern -AllMatches

This example extracts text between HTML tags, it uses the Select-String command with the -AllMatches parameter to find all matches of the pattern in the input string $original_string.

All of these methods can be utilized to extract the text between strings in PowerShell.

The following examples show how to use each one of the methods.

Extract Text from a String Using the Substring() Method

The Substring() method of the String class is used to extract text from a string.

The following PowerShell script shows how to use it with syntax.

"Hello, World".Substring(startIndex,length)

The Substring() method takes the startIndex and length to extract the string from the given string.

The output of the above PowerShell script is given below.

Hello

Extract Text Between Brackets in a String

The following example shows how to extract text between brackets in a string.

# Define the input string
$original_string = "This is [dummy text] inside brackets [and another] bracket."

# Define the regular expression pattern to match text between brackets
$pattern = '\[(.*?)\]'

# Use the Select-String cmdlet to find matches
$matches = $original_string | Select-String -Pattern $pattern -AllMatches | ForEach-Object { $_.Matches }

# Extract the captured groups from the matches
$matches | ForEach-Object { $_.Groups[1].Value }

In this example, the $original_string contains the text enclosed in brackets. We have defined a regular expression pattern [(.*?)] to match text between brackets.

The Select-String cmdlet finds all matches of the pattern in the input string $original_string and stores the matches in the $matches variable.

Finally, we iterate over the $matches variable using the ForEach-Object to extract the text between brackets.

The output of the above PowerShell script is given below.

dummy text
and another

Get text between parentheses

The following example uses the regular expression to get text between the parentheses.

$original_string = "Format-Table (FT) cmdlet in PowerShell formats the output as table"

$pattern = "\((.*?)\)"  # Regex pattern to match the content within parentheses

$match = $inputString | Select-String -Pattern $pattern | % { $_.Matches } | % { $_.Groups[1].Value }

$match

In the above PowerShell script, the $original_string contains the string enclosed in parentheses.

The $pattern variable stores the regex pattern. We then use the Select-String cmdlet with the -Pattern parameter to match the input string $original_string against the regex pattern.

It extracts the value of the text between the parentheses and stores it in the $match variable.

The output of the above PowerShell script is given below.

FT

Get Text Between Quotes

To extract text between double quotes from a string in PowerShell, we can use a regular expression.

The following example shows how to do it with syntax.

# Define the input string
$original_string = 'This is "dummy text" between double quotes.'

# Define the regular expression pattern
$pattern = '".*?"'

# Use -match operator to find matches
if ($original_string -match $pattern) {
    $extractedText = $matches[0].Trim('"')
    Write-Output $extractedText
}

In the above PowerShell script, we defined the $original_string that contains text enclosed in double quotes. We define regular expression patterns “.*?” to match text between double quotes.

We used the -match operator to match the pattern against the input string $original_string. If a match is found, the matching text is stored in the automatic variable $matches.

Finally, we output the extracted text between double quotes.

Output:

dummy text

Get text between HTML tags

To extract text between HTML tags in PowerShell, you can use regular expressions.

The following example shows how to do it with syntax.

# Define the input string containing HTML
$original_string = '<p>This is <b>just a random </b> text between <i>HTML</i> tags.</p>'

# Define the regular expression pattern to match text between HTML tags
$pattern = '(?<=<[^>]*>)(.*?)(?=<[^>]*>)'

# Use the -match operator to find all matches
$matches = $original_string | Select-String -Pattern $pattern -AllMatches

# Extract the matched text from the matches
$extractedText = $matches.Matches.Value
Write-Output $extractedText

In this PowerShell script, the $original_string contains the HTML tags. The $pattern variable stores the regular expression to match text between HTML tags.
We used the Select-String cmdlet with the -AllMatches parameter to find all matches of the pattern in the input string $original_string.

Finally, we extracted the matched text from the matches using $matches.Matches.Value and output the extracted text in HTML tags.

The output of the above PowerShell script is given below.

This is
just a random
text between
tags

Conclusion

I hope the above article on how to extract text from a string in PowerShell using the various methods is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.