EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial Regex in PowerShell
 

Regex in PowerShell

Updated March 23, 2023

Regex-in-PowerShell

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

regex in powershell 1

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:

example1

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 2

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 3

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:

regex in powershell 5

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:

regex in powershell 6PNG

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:

regex in powershell 7 PNG

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:

regex in powershell 8PNG

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 –

  1. PowerShell String Functions
  2. PowerShell Interview Questions
  3. Python Regex
  4. VBA RegEx

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW