PowerShell & RegEx Getting Started Tutorial
4K views
Aug 30, 2024
Harness the power of RegEx inside PowerShell! RegEx is a brilliant way to search, match, and manipulates strings based on specific patterns. ? Get the Code Here! ? https://github.com/JeffBrownTech/jeffbrowntech_youtube/blob/main/2024-08-28-PowerShellRegEx/PowerShellRegEx.ps1 ? Read More About This ? PowerShell RegEx: Getting Started Guide https://jeffbrown.tech/powershell-regex/ ? Watch: https://youtu.be/ikLEDTzljXE?si=UoD9M55p4bY7uaAP ? Reg101 Website https://regex101.com ⏰ Chapters ⏰ 00:00 Introduction 00:26 RegEx resources 01:34 Select-String 02:58 Match operator 05:21 $Matches variable
View Video Transcript
0:00
What's up everyone, today we're going to talk about PowerShell and regular expressions or regex
0:04
I'll show you a couple of different examples of how to use regex within PowerShell
0:09
It might be a script or just a one-liner that you're running
0:12
I recently wrote an article about this, I'll link to that down in the video description below
0:17
And as always, this code is available out in my GitHub and that will be linked below also
0:22
My name is Jeff, you're watching Jeff Brown Tech, let's go ahead and get started
0:26
To talk about first, what exactly is regex? Regex is a way to represent a string using literal or special characters called metacharacters
0:35
And you can use this format to search for strings inside of another string, inside of a file
0:42
Basically parse information as needed. If you're coming from the Linux background, it would be similar to using grep
0:49
Now I am not a regex expert. I have done some regular expression work in the past when I did Skype for Business and Microsoft Teams things
0:56
when normalizing phone numbers that people may dial into their phone and, you know
1:00
turning a 7-digit phone number into 10 digits or adding country codes, things like that
1:05
But as far as using it for strings, I don't use it that often. Maybe some PowerShell parameter validation, which I've talked about before in another video
1:13
There are plenty of resources out there, YouTube, other blog articles, chat, GPT
1:18
You could go to it and say, I need a regular expression for this and it'll probably give you something really close
1:23
If you want to play around with regular expressions and learn more about them in some tutorials
1:28
a website I often use is regex101.com. I'll include a link to it down below
1:34
Anyway, let's go back to the code and let's start talking about regular expressions
1:38
The first example is using selectString. And we are going to use selectString and a pattern to find certain lines inside of a log file
1:47
And over here on the right, I have a log file. It's got some different entries in it for info, warning, and errors
1:54
And let's pretend this file is a lot bigger and I want to go through and find all the lines that start with error
2:00
Let's take a look at our command we're going to run on line 2. We're going to run getContent
2:04
We are going to give it the path logFile and then pipe that over to selectString and give it a pattern
2:11
Let's take a look at our first regex pattern here. The caret symbol there, the up arrow, means we're looking at the beginning of the line
2:19
So we don't care if error occurs within anywhere else in the line. I'm only concerned with when it begins at the beginning of the line
2:26
And then I have an all caps here error and that's the string that I'm looking for
2:31
We'll take this line and go ahead and execute it. And you can see it outputted two lines here
2:36
They are exactly the same ideally. You know, a real log file, they might have different errors listed in them
2:41
And if we look at our actual log file, there's line 9 and line 15
2:45
It only occurred in the file twice. Combining selectString with a regex pattern is really powerful if you want to search through a file for specific strings, error messages, whatever else you need
2:58
Continuing on, let's take a look at the match operator and the automatic variable called matches
3:04
On line 6, we have an array here of email addresses. You'll notice different email addresses here of domains
3:09
We have Contoso, Fabrikam, Parts Unlimited. Find specific email addresses for a domain
3:16
In this case, it's going to be Contoso.com. On line 21, I have a variable to find for pattern of the pattern that we want to match
3:24
Let's go through the regex on this real quick. Again, we have the caret symbol, the up arrow, meaning we are looking at the beginning of a line
3:31
Then we have our first capture group. You'll see what these capture groups are coming up later, but that's within the parentheses there
3:37
We then have a period, meaning we're looking for any character. We want to match any character, so underscore dashes
3:44
Probably includes a lot of things you wouldn't find in valid email addresses, but we're not worrying about that right now
3:49
Then we have an asterisk, meaning we want to find zero to unlimited occurrences of any character
3:56
Probably should be one to unlimited, but again, not looking for perfection here, just getting something quick
4:03
Outside of that first capture group, we have just a literal at sign, no other special characters
4:08
It doesn't mean anything else, because that's what an email address looks like. You have the username at something
4:13
Then we have our second capture group in parentheses, and we're literally just looking for Contoso.com, so that's what we want to match on
4:21
Then we have the dollar sign, meaning we're only looking for this at the end of the line
4:27
Sandwiching this between the caret, the up arrow, and the dollar sign means we are only looking for that on the whole line that we're searching
4:36
We're not interested in anything else. The line that we're searching has to just be an email address
4:41
There cannot be any other strings or spaces around it. We'll go ahead and bring our pattern into our session
4:49
Then what you can do right here on line 24, take your email addresses array
4:54
We'll run it through the match operator and give it the pattern that we're wanting to match on
5:00
If we run that, we get back two responses, Jeff and Angela
5:05
Look back at our array that we defined here for highlightContoso.com. We can see it matched those two
5:11
You don't even have to run through a for each, checking each one that matches
5:16
It'll just compare everything in that array and output the results. Along with the match operator comes the matches automatic variable
5:25
This is a variable you don't have to define it. It's automatically included whenever you do a match operation
5:31
What we can do here is iterate through our array starting on line 28 for each email and email addresses
5:40
We'll say if the email matches the patterns to our current item that we're looking at
5:45
we're going to create a custom object and split that email address into a couple of different parts
5:52
On line 31 is the first attribute, which is the whole email address. If we look at the matches variable and then the zero index, that's the whole email address
6:03
Then lines 32 and 33, we have the username set to index one and then domain at index two
6:10
These match up with our capture groups that we created in our pattern inside of each parentheses
6:15
Then we're just going to output the object. Let's run through this for each real quick and see what it outputs
6:22
Here we have email address, then we have username, and then we have domain
6:27
Now let's break down exactly what this matches variable is doing when we run it through a match operator
6:33
Hopefully, maybe this will make a little more sense. I have a note here just saying matches is a hash table containing the results of the most recent
6:41
successful regular expression match operation when using dash match. The first entry in that hash table contains the entire match string
6:50
So whatever matched your pattern is going to be in that first element
6:56
And then matches one, two, and so on match any strings inside those capture groups inside of your regular expression
7:04
It goes on to say the index corresponds to the order of the pattern capture groups
7:09
Let's run through each part individually. Let's go back to our email addresses array
7:14
We're going to look at the first element, which is going to be my email address here, jeff at contoso.com
7:21
And let's match it to our pattern. And it does output true just saying it does match the pattern
7:27
So let's go back to matches and look at the full hash table
7:31
And we can see several key value pairs here. We have zero, which is the full email address that it matched
7:37
Number one is then Jeff, which matches our first capture group back from our pattern, this part right here
7:45
And then we have number two, which is contoso.com. And that is what is inside the second parentheses, just a literal match to contoso.com
7:54
And then you can output each part individually. If we just look again in at zero, that's the full one
8:00
Matches is then the username. And matches two is the domain name because it matches up with our capture groups
8:08
This is useful just like in this example if you're wanting to go through a list of email addresses, find a specific email address
8:15
and then break apart each part of the string into the username and the domain name
8:19
Definitely other ways of doing it without using regular expressions. You could split the string on the at sign
8:25
You could do like wild car, contoso.com, things like that. But using regex and the match operator kind of just takes care of a lot of it automatically if you set up your pattern correctly
8:37
Next, we have the replace operator. And you can use regex to replace parts of your string that you're looking at
8:44
For example, let's say you have a phone number here that somebody's inputted. It has parentheses around the area code, spaces, and a dash between the different parts of the phone number
8:54
You can use a regex to replace any non-digit characters. So we have our phone number here in our example
9:00
We'll put that into our session. And then we are going to say phone number and then our replace operator slash d represents any non-digit characters
9:11
And then we're going to replace it with absolutely nothing. So this last part here is just two double quotes with nothing in between them
9:18
And if we run that, you can see right down here we have 555-456-7890 without any of these special characters or spaces in it
9:27
Finally, we can use validate pattern and regex to make sure data given into our parameters on our scripts or functions matches what we're expecting
9:37
In this case, I have a function called send message. We have validate pattern for a basic email address
9:43
And then on line 67, we're just going to output the email address
9:46
Let's bring this in here. Send message. I have a previous example here
9:51
I have an underscore instead of an at sign. So this is going to come back and give me an error
9:56
You can customize this error message inside the validate pattern definition. I have another video where I talked about different ways you can validate your parameter input
10:05
I'll include a link to it down below. That does it for this video
10:09
There are lots of other ways you can use regex inside of PowerShell and other operations
10:14
These were just a couple examples that I came across and what I would most likely use them for
10:19
If you use regex in creative ways or anything like that, leave a comment down below
10:24
Always great discussion for other people to see. As always, thank you for watching
10:28
We'll see you next time
#Computers & Electronics
#Programming