PowerShell Quick Tips : RegEx Regular Expressions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this Powershell quick tip video in this quick tip video we're actually going to be taking a look at regex inside Powershell now I'm not going to be going super into detail of regex I'm going to assume some sort of uh knowledge of regex but I will be covering uh some of regex if you guys would like a full video on just write a regex which is regular expression uh please let me know in the comment section down below and I can definitely do a more fully detailed explanation of just regular expression on its own because regular expression isn't just used in Powershell it is used in various other programming languages pretty much any programming language that you can really imagine you can really use regex or regular Expressions uh there is a very good site for regular Expressions that I use and then I'll be showing you guys on this video um which is the regex r um so this is what I use to test out my regular Expressions before I put them into my code um so I'll be showing you guys how I kind of use this to help me and show you guys what you can actually do on this website to actually more easily develop your regular expression patterns that you're going to be using in Powershell or anything else as well but if you guys do want a little bit more of a detailed video on Regular Expressions once again just let me know in the comment section down below and let's actually get started with the regular Expressions here so what we're going to want to do is I actually have two files here so we have two files with just some text in there and I've put in some fake Social Insurance numbers that's what we have here in Canada and a social security number which is what the United States has and I I don't know other countries have this as well but these are the two that I've used for our examples today since they are slightly different I want to be able to uh for us to be able to detect the differences and then in the second file here I actually have an email address I have this series of characters which is the amount of characters that you would have in a credit card number and then I just have the name of the channel here but these are two separate text files so we're actually going to be able to use some regular Expressions to actually find data in our text files so let's go ahead and let's just actually play around with regular Expressions uh just a little bit here just to kind of see what it all entails um so the first video we're going to want to do is uh create a string here that we can play around with before we actually get into our files so let's just create a variable called test string here and I'm just going to make this equal to a my name is jacked programmer I love Powershell so we have a pretty simple string and um let's go ahead and let's just put in a series of numbers here just one two three four at the end and this we should be able to actually do quite a few simple examples here so the way that we would actually use regular Expressions to match the string there's actually a couple different ways that you can do it in Powershell I'm going to show you two ways that I find are probably the easiest and most um intended way that you can really do it so the first way is we're going to take our test string variable and then we're just going to do a space and then we're going to go ahead and go ahead and do a match parameter here and then we can go ahead and already put our regular expression in here so what you can actually even do is not even really use regular expression but just put in the word jacked here and if we go ahead and we look at this we actually get a true so the word jacked was in our string which we can of course see that very well um and then if we do a jacked with two D's we will find out that it is not true so if we actually go back to just put it with the D here what we can also do in Powershell is we have a variable called matches which actually stores all the matches here so if we actually look at that we can actually see the value was actually matched on jacked here so that is one way to do it now the way that I actually prefer to do it here is we actually take our string we pipe that to a select Dash string and we pass in a pattern and we can go ahead and pass in the exact same pattern here and if we actually go ahead and we look at this here we will see that it returns us the entire string and the pattern that we actually wanted is highlighted so I actually find that is very very nice with the select string that's kind of why I prefer this way but definitely both ways do work now another thing that you can actually do with regular expressions and Powershell is you can find let's say all the words that are over uh four characters long so what we're going to do is we're going to do a test string we're going to do the select Dash string we're going to do a pattern so now this is going to really get into regular Expressions here so we need to put in our pattern so to match a any character what that'll actually be is it'll be a backslash w and this will be any word character so this will actually include quite a bit it'll include all your numbers and all of your outlook characters as well so A to Z regardless of capitalization it will actually match and the numbers zero through nine and we can actually see this as well as if we go into our website here the Reg xer and we can actually look at the cheat sheet here and if we look we can actually see that the W actually does highlight everything here and we can actually go uh that's the cheat sheet regular expression character classes and we can say word and here it will actually show us the equivalent regex forges slash W so it'll match everything from uppercase a to uppercase said lowercase a to lowercase it said 0 through 9 and then the underscore as well so that is a easy way to kind of know what the slash W is and if you do a slash capital W it'll be any character that is not in this set um so just be aware that you can do quite a few things with the built in presets with regular expressions so if we actually just try this right now we're going to see uh that we actually only get one match and it is M but we know based on our website here it should actually match every single character in the string really and that is where the parameter all matches will come in with the select string command lit and now if we actually go at it there it is it highlighted every single character we have except for the comma because we know that comma is not part of the slash W set now let's say we only wanted words that have four or more characters in it so we want name jacked programmer love Powershell and these numbers we would actually add a curly bracket here curly bracket and then all we would do is do a four comma and we can leave it blank now if you did have a specified Max number let's say we only wanted words between four and five characters long so we don't want jacked programmer and we don't want Powershell this is what you would actually do and if we actually go ahead and we actually still find it because it is still at least four to five characters long and we can have uh so we have five here and then we have another four or five here and then we have another five here so that's how it breaks it down I will actually be showing you guys how to do the boundaries as well so now that we only want to match the name the word name love and one two three four what you would actually have to do is add these boundaries so that will actually be a slash b and a slash B at the end that will make sure that there is a boundary at both ends of the string so if we actually go ahead and input that there it is we have just name love and one two three four now let's say we only really wanted to match the strings themselves like the actual just words we don't want the one two three four what we would actually have to do is change this slash W to be in a dash said all upper caves and then an a dash said all lowercase now in our case all we really need is the a dash Z lowercase because we don't have uppercases in here um but we would of course want to match on if there's anything uppercase if we ever needed to so if we actually run this now we only get those two words that are highlighted the number isn't highlighted the longer words aren't highlighted or anything like that but if we all of a sudden Wanted let's just get anything over four characters we can remove that five and we can actually grab all things that are over the link for or over it will actually highlight all those batches and if we again if we remove the all matches it will only return the first match that it finds so I always find myself making sure that I put the all matches in there just to be positive of what I am binding so that's for these simple strings now let's actually go ahead and see what we actually found in here so if we actually look we can see that they're all highlighted but what we can actually even do is store that in a variable here so let's store that into results and what you can actually do is if you look at results afterwards we're going to see it's not that different but what we can actually do is a DOT matches in a DOT value and if we actually look at this here we actually get only the four words that were matched so we can actually then programmatically take all the matches that we got and maybe do some sort of other algorithm that we need to do with those maybe we want to find all these things in one file and then look for those exact words in other files or files that start with that are named one of those things those are all things that you can actually easily do with the select string so let's actually go ahead and let's test out regular expressions with our files here so let's go ahead and let's create a variable called path and we're going to go ahead and we are going to just take the path of our folder here because we want to grab all the files as always we like to do that with the get child items what I like to do is I like to just create another variable here called files and we're going to do a get child item we're going to pass in our path here now even though I know that we only have one level in here I always like to add the recurse just in case maybe we add some folders in here later on and some more text files we always want to be able to actually search those and not really have to modify our whole script again so let's go ahead and let's do that now there are multiple ways that we can actually use regex with files or regular expressions with files so let's actually see some simpler examples and then we're going to go into some a little bit more complicated examples so let's do the first one here where we are actually just going to take the file as a variable itself and we are actually just going to pipe that to a select string and we're going to pass in a pattern and the pattern that we're going to pass in is a very simple pattern all we want to find is three numbers in a row um so as We Know we should actually have both of these files coming back because we have numbers in both of them so let's go ahead and let's run this here and there is our result so what is neat about when you have a file and you're passing the files to the select string it will actually tell you exactly what file it found it in the line number that it found and then it shows you the line that it founded on as well as of course the highlighted portion that you're looking for now once again as you notice it only found one match even though there are matches here you do still need that all matches to be able to find all the possible matches and there you go now all of them will be highlighted so there is all of our matches here for all these uh digits so as long as there's three digits that follow each other we want to pull a match so we can see that it did pull them all up gave us the file names and the line numbers for those items now let's actually go a little bit more specific and say that we want to find all the credit card numbers now as I've mentioned this is the exact numbers of a credit card number now as we know credit card numbers might be written like this they might be written like this they might even have some dashes here so let's go ahead and let's write a simple regular expression that will help us find that so let's do a bios and we're going to do a select Dash string and we're going to put in a pattern here and our pattern what we're going to do is we know that it is it's basically four numbers four numbers and then four numbers so let's go ahead and let's do a four numbers and then or numbers and then another four numbers all right so now if we actually go ahead and we look at this we do find our credit card number but we're just going to do the all matches just to be sure we do find that credit card number now the only thing is now if I actually manipulate the way that that credit card number is written we will actually notice and we just have to actually get the uh we have to re-resh our variables here to make sure that we get the new spelling we'll actually see that we don't actually find it I would actually have to add the spaces in and now if we look at it it will actually find it but now we have the problem of if there's no spaces it wouldn't be found so we actually need to build some sort of regular expression that will help us determine whether or not it works for any type of situation now this is where I typically use the regexer website I don't actually know the actual name of this website that's always what I just called it here but here is the regular expression here so we have our files here so let's go ahead and let's just copy paste this expression in there and there it is so we don't find it but as soon as we put the spaces in there we can see that it does highlight it so what we actually need to do is we need to say if there's a white space or if there's a dash or if there's none of those then actually keep track of that so what we actually want to do is in between the four W's is we're going to add a set of parentheses and then what we want to do is add another set of parentheses inside and inside the nested parentheses we're going to do a backslash s which is a white space character so this will match any white space character and then what we're going to do is we're going to do a pipe here which is going to be the or a dash and then outside of the nested parentheses we're going to put a star so this will actually mean if there's a white space or a dash or neither so if there's zero or more entries of this it will actually find it so then what we can actually go ahead and do is go ahead copy paste this into here and there it finds it and now if we actually remove the spaces it finds it as well if we add the dashes it will find it as well so what we can actually do is copy this back into our pattern here and if we go ahead and we look for it we can actually find it and if we put it back to how it was before and we go ahead and we run this entire thing we will see that we still find it now of course this isn't perfect this will not find all credit card numbers the nice thing about this website as well is that there are Community patterns and there is pretty much a pattern for anything that you can really think of unless it's very custom but there is a credit card um regular expression as you can see it will actually find all sorts if there's dots separating the numbers or anything like that now of course I actually made my credit card I just noticed I actually forgot four numbers it should actually be 16 digits total um but you guys do see that we can easily have this pattern here just like that now there is one thing about this website here is if you go ahead and copy this entire pattern it will not actually work same thing on is this one is a little bit better because you can't highlight these gray characters but if you do highlight a pattern from here which I'm going to show you guys in just a second uh it will actually not work you do have to do some slight manipulation in just removing the slash g at the end and the slash at the front for it to work in Powershell but after that it works just fine um so let's go ahead and let's take a look at some other uh just some simple examples here with the boundaries and the social insurance number so if we actually try to find the social insurance number here let's do a select Dash string uh patterned and let's go ahead and let's put in our pattern here which the social insurance number is three numbers separated by dashes so let's go ahead and let's find that now as you can see we do find that not a problem here and we're going to do all matches and it gets found pretty easily now as we know we probably shouldn't have these dashes directly in there in case someone doesn't put dashes so let's go ahead and let's just put our custom thing that we've made here which basically says if it's a white space or a dash or nothing still find it and if we actually go ahead and we look for this we will actually see that we actually got a false positive on our social insurance number because it's also pulling up our credit card number so that's where again we can add the slash B at the front and the slash B at the end and now if we go ahead and we run this it will only find our social insurance number well that is perfect that's how we could easily find it now another one that might be very useful that you might use in your everyday life well maybe not every day but uh fairly often in a production environment is maybe email validation here so we do have an email in our file so maybe we want to find if someone puts their email in there because maybe uh what we have done is we've taken a whole bunch of files from somebody and they know that they've written their password for their email somewhere but they don't know where they might have probably put their email in that file right above the password so if we can find where their email is we can maybe find their password as well now of course I don't recommend ever doing this don't ever put your password in a file with your email in it that is not very secure but if you do have that situation ever arise this is what you can do to easily find where the email is so let's go ahead just copy paste this line and we have a fresh pattern now as I've said before is this website will have a lot of patterns that are already pre-made and email is definitely one that I don't really like to write myself uh it is quite long as you can actually see down here this is the actual pattern so what I like to do is let's just actually highlight the entire thing and let's copy it and the only thing that will actually cause an issue here is we do need to use double quotes because you are allowed to use a single quote in an email but at least that's kind of what is in the regular expression I believe that they actually have it as it's a not allowed character so there we have it so we can actually have this pure email validation here it looks very very complicated and that is another thing about regular Expressions that I've read in the past as well is they don't recommend it for your production scripts always try to use something other than regular expression if you can it is very CPU intensive and also very very hard to read if you come back to it later on unless of course your code is very commented properly it could be very confusing for the next person to go read it so if we go ahead and we run this line we will see that we actually didn't get anything but we know that this is a fairly good expression so if we take out the slash at the beginning and the slash g at the end and we go ahead and we run it our email actually comes up jimbob gmail.com so that actually did work properly so there is one other thing that we can actually do here uh and then that is actually kind of scanned each file and see if there's any type of sensitive information in it so if there's a social insurance number a credit card number or a phone number or something we can actually just kind of shoot a little warning and say hey this file actually has this information in it uh so this would actually be a programmingly like a actual script example where you can actually use your regular Expressions to scan files so let's go and do a four each file in files and let's go ahead and open and close the curly brackets here well the first thing we're going to want to do is actually create a variable called content and we're going to do the get content on the actual file here so let's do a path and then file dot full name and then what I like to do is I like to just do a right output looking at file and then we're going to go ahead and put the file dot full name here and then what we can do is actually use a switch statement afterwards and now the switch statement actually has a regular expression or regex parameter and then we're going to put in the string that we want to evaluate the regular expression on and we can do an open and close and curly bracket and we can actually put all the different patterns that we want to match it on but let's go ahead and let's just grab our first one here which is our made up credit card number of course because we don't have 16 digits in here but let's go ahead and let's open the case here and we're going to do a right warning and let's write the warning of file make contain a credit card number and we can do another one here which is going to be this one here which is going to be it may contain a syn number let's see if we do that and let's just copy this warning here may contain a a Cent and now if we actually copy the same exact one here we're going to have a different one which is going to be the social security number which is three characters two characters and then four characters there it is there and let's go ahead and let's do a uh what else is in here let's find that is fine as well um yeah let's just do this so this will tell us if a file contains a social insurance number SSN or credit card number and then let's just do a default here which is going to be right host we're going to do a right host here because what we want to do is we want to do a foreground color which is green and let's just say file is okay but we go ahead and we launch this here [Music] um yes so it is actually looking at each line here so we're not going to want to actually do this default here that is fine so let's go ahead and let's look at this so here we have in test one we have here that it may contain a social insurance number uh which we know it does and it may contain a social security number which we know it does as well and then the second file will actually contain a credit card so that is actually perfect as well so what we can actually do here is if we have a lag equals false flag here what we can do is if these ever come up we can actually put the flag to True here so if it ever finds any of these we can actually flag it as true and then if lag is equal to false still at the end this is where we can actually say right post foreground color is green and then we can say file is okay and now let's go ahead and run this here this for each Loop and there it is obviously we didn't get any file is okay because we don't actually have a file that is actually safe so let's add a file here we're going to do a test three dot text this is a safe file and let's save that and let's go ahead and let's reload our files here and let's go into the for each Loop and we should have the test one is not safe because we do have some warnings test two is not safe because we have some mornings task 3 is okay there's no data in there that we need to worry about doesn't have a social insurance number Social Security number or a credit card number so this is a very good way um to actually scan maybe uh your entire entire file server to make sure that your users aren't storing very sensitive information in their files maybe if you're working for a company that deals with a lot of Finance data you wouldn't want credit card numbers to just be in plain text or if you're dealing with help um details maybe they have some health card numbers or some some social insurance numbers or social security numbers this is a great way that you can scan your entire file repository and make sure that there's no sensitive information in there with Powershell so that is really the basics of regular expressions with Powershell if you guys would like to see more of these types of videos with regular expression please let me know in the comment section down below it is a topic that is opt-in very skimmed over but is actually can be very very detailed and is sometimes very useful depending on the situation that you have at hand also please be sure to check out the website in that description I'm going to post that link to the website in the description so you guys can test out your own regular expressions and also be sure to hit that like button hit that subscribe button also don't forget to hit that notification Bell to be notified when the next video comes out and I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 2,082
Rating: undefined out of 5
Keywords: powershell basics, powershell, windows powershell, programming, scripting, powershell scripting, powershell scripting tutorial, powershell tutorial, how to, powershell api responses, powershell automation, powershell beginners, powershell variables, quick tips powershell, quick tips, learn powershell, powershell for beginners, powershell commands for beginners, regex, regex powershell, regular expression, regular expression in powershell, regex tutorial, regular expressions
Id: -au9xkXpVOU
Channel Id: undefined
Length: 32min 3sec (1923 seconds)
Published: Mon Sep 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.