Powershell, Regular Expressions And You!


Pretty scary, isn’t it? While most of you will never have to build something this long and complex, it still is a nice idea to learn how to use Regular Expressions in your scripts when you need to look for patterns in the contents of a log file, enforce a specific pattern for something like a phone number or login name.  Whatever the case may be, Regular Expressions can save you a lot of time in either of these situations.  The only problem is…Where do you begin?  Hopefully the my posts on this subject will give an idea on where this can take you.  Please understand that I am no expert on this subject and this is by no means an advanced take on the topic.  My only goal is to get you started on using Regular Expressions and showing some potential “Real World Examples” that will hopefully help you out.

By the way, the expression shown at the beginning is used to verify IPv4 addresses.

Let’s just start off with something more simple, such as a phone number. For instance, you want to enforce in your script to build user accounts in Active Directory that the phone numbers need to match the following: “555-5555”. Seems simple enough, right? Looking at this number, we see three things that we need to enforce:

  1. Prior to the “-” we need exactly 3 digits
  2. We need to make sure that after the 3 digits, there is a “-” before the next set of digits.
  3. Lastly, after the “-” we need exactly 4 digits

How we would do that is setting the Regular Expression as such: “^\d{3}-\d{4}$”

Lets break this down to understand what each part in this plays.

  1. First, we encase the expression in quotes “” so it knows to where the expression begins and ends.
  2. The “^” (carrot) tells the expression that the beginning part of the string to be matched must match the beginning of the expression. In this case, the beginning must match 3 digits.
  3. Next is the “\d” which is used to match a digit, usually something like “\d[0-9]” or “\d{2}”
  4. The “{3} after the digit tells the expression that it must be 3 digits in length, no more and no less.
  5. The “-” tells the expression that there must be a dash after the first 3 digits and before the following 4 digits:
  6. Again, we have the “\d{4}” which tells us that there must be exactly 4 digits after the “-“.
  7. Lastly, we have the “$” which tells us that it must end with the 4 digits, otherwise it will be considered invalid.

Let’s put this into a more “Real World Example”. Lets assume this piece of code is in a script to build an Active Directory account:

[regex]$phone = “^\d{3}-\d{4}$”

Do {
Write-host -ForegroundColor Cyan “Please Enter a Phone Number that matches the following: 555-5555”
$number = Read-Host
If (!($number -match $phone)) {
Write-Host -ForegroundColor Red “Phone Number: $($number) is in incorrect format! `nPlease try again!”
Until ($number -match $phone)
Write-Host -ForegroundColor Green “Phone Number: $($number) is in proper format”

Using this code, you can see when running the script that the user cannot continue on with the build until they put in an acceptable phone number.

As you can see, only a phone number with the correct format is acceptable.

This ends my first part in a multi-part series on using Regular Expressions with Powershell.  Next time I will dive into using letters and ranges of numbers and letters for validating content.

About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
This entry was posted in powershell and tagged , , . Bookmark the permalink.

4 Responses to Powershell, Regular Expressions And You!

  1. Pingback: Creating a Report of Log File Data using Regular Expressions, Arrays and Hash Table | Learn Powershell | Achieve More

  2. lipkau says:

    This is shorter:

  3. lipkau says:

    Hi Beo,

    since you are talking about RegEx, I wanted to share this link:

    It came in handy a couple of times for me.

    By the way: I linked this blog of yours in my tentative to make my own blog 😛


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s