EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial Regex in PowerShell
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Regex in PowerShell

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.

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,060 ratings)

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
Popular Course in this category
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)
  41 Online Courses |  13 Hands-on Projects |  322+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more