Updated March 23, 2023
Introduction to Regex in PowerShell
A regular expression is a special character combination which helps us to find different and difficult kind of data from text and any string. They can be used to search, edit, or manipulate text and data. Since PowerShell build on top of .NET, so all regex syntax of .NET supports PowerShell also. Let me give you some examples, suppose you have a long string containing numbers, letters and special characters and you wanted to know the start position of number than you can use regex expressions. Again suppose you have many users list but you wanted to see only those users having “sh” as the last string. example “Akash”,” Vikash”,” Rakesh”,” Suman”,” Amit”,” Anish” and we want to see only those name having “sh” as substring, so the names will be “Akash”,” Vikash”,” Rakesh”,” Anish” as all of them contains “sh” as substring.
Explain Regex Metacharacter in PowerShell
If you have already worked with the various script of PowerShell or you have already developed or scripts, you will typically have come into contact with regular expressions.
Syntax:
$some_Data = @('Ranjan', 'Anish','Ranjana',”Anjana”, ”Sanjana”)
$some_Data | Where-Object {$_ -match 'Jan'}
Here, we are creating an array of strings and then we write a query that displays ”Ranjan”,” Ranjana”,” Anjana”,” Sanjana”, it will not show “Anish” because the “Anish” does not match the Jan pattern. The -match operator can also be used without the Where-Object cmdlet.
'Ranjan' -match 'Jan'
It will return the True as “Jan” is a substring of “Ranjan”,
'sh' -match 'Ranjan'
It will return False as “sh” is not a substring of “Ranjan”.
In the below example we are replacing “good boy” with “bad boy. So to replace first it compares and it matches then replace it.
'Ranjan is a good boy' -replace 'good boy', 'bad boy'
Output:
Here we can see from above, after -replace command we are getting the output as “Ranjan is a bad boy”. The “-replace” operator performs multiple operations like finding the matching string and then replacing it. Thus, the uses of PowerShell regular expressions can be explained as they are mainly used for comparing and replacing the match string.
Examples to Implement Regex in PowerShell
Below given are some examples:
Example #1
Here we are going to match exact characters available anywhere in a given string.
"This is home town " -match "home"
“This is your hometown” -match “town”
“It's raining today” -match “home”
Output:
Example #2
Let us see little more different types of example here, suppose we want to check any letter match or single letter match
"Ranjan" -match "R"
"Akash" -match "A"
"Vikash" -match "R"
Output:
Example #3
In this example, we are going to match the rage value. The use of a hyphen (-) allows you to specify an adjacent character.
"Ranjan" -match "[A-Z]anjan"
"Ranjan" -match "[A-Z]"
"Ranjan" -match "[i-k]"
"Ranjan" -match "[k-p]"
"Ranjan" -match "[k-k]"
"Ranjan" -match "[k-l]"
"Ranjan" -match "[k-o]"
"Ranjan" -match "[k-p]"
In all examples, it is checking if any letter of “Ranjan” comes under its rage [k-l],[k-o],[k-p] or not. If defined range comes under a given string then it will return true else false.
Output:
Example #4
Here we are matching all characters except those inside the brackets. Suppose we have a list of registration numbers but we want to see all except 2008 batch students registration, then we can use these types of matching.
"2009students" -match "[^2008]students"
“2009students" -match "[^2008]students"
“2008students" -match "[^2008]students"
“2012students" -match "[^2008]students"
“2008students" -match "[^2008]students"
“2011students" -match "[^2008]students"
Output:
Example #5
In this example we are going to match the beginning of characters, let us assume that we have many users and we want to check if this user contains specific characters at the beginning which we want to check
"Ranjan" -match "^Ra"
“Suraj” -match “^Su”
“Bhanu” -match “^Bh”
“Anny” -match “^Ki”
“Vikash” -match “^Ji”
In the above example, we are checking if the given string starts with “Ra” for name “Ranjan” which was true, and in the same way, “Su” from name “Suraj” which was again true, but was not true for “Ki” for name “Anny” name Anny starts with a “An” not with “Ki”.
Output:
Example #6
Now we are going to match end characters, it will check if the end characters are matching the given expression letter or not.
"Ranjan" -match "a $"
“Suraj” -match “aj$”
“Bhanu” -match “hu$”
“Anny” -match “ny$”
“Vikash” -match “up to $”
In the above example, we are checking if the given string ends with “an” for name “Ranjan” which was true, and in the same way, “aj” from name “Suraj” which was again true, but was not true for “hu” for name “Bhanu” name Bhanu ends with “nu” not with “hu”.
Output:
Example #7
Till now we have only discussed matching sequential terms or expressions, now suppose we want to check the existence of letters anywhere inside the string with nonsequential. For example, suppose you have to check if the string “Hello friend I am a software engineer” contains “hs”. So we can see in the given string character is there but nonsequential.
$string =“Hello friend I am a software engineer”
$string -match "[Hs]"
$string -match "[ip]"
$string -match "[se]"
$string -match "[ls]"
$string -match "[zz]"
In the above example if any letter matches it returns true irrespective of the position of the letter, many times we can face such type of situation where we may need to know the existence of letter strings
Output:
Conclusion
If you take a close look at the use of PowerShell regular expressions, then you will find that it can solve an enormous amount of problems on the admin level. Like finding match letters on either start or end position or finding letters anywhere inside the strings.
Recommended Articles
This has been a guide to Regex in PowerShell. Here we discuss the introduction, syntax and Regex Metacharacter in PowerShell with examples. You may also have a look at the following articles to learn more –