Use PowerShell to Look for Phone Numbers in Text

Use PowerShell to Look for Phone Numbers in Text

  • Comments 5
  • Likes

Summary: The Scripting Wife learns how to use a regular expression pattern to look for phone numbers in text files.

Microsoft Scripting Guy, Ed Wilson, here. I am looking over some of the slide decks from the Columbia, South Carolina SQL Saturday #70 event. There were a couple of really good Windows PowerShell sessions during the day (including the one that I presented on Windows PowerShell Best Practices for database administrators (DBAs).

I am midway through Brian K. McDonald’s SQL Reporting presentation deck, and I sense the presence of another person in my office. I am careful not to move my head, but I look out of the corner of my eye. Sure enough, it is the Scripting Wife (but then, we are the only two in the house—but it could have been a poltergeist). I decide I will wait to see what she says. Silence begins to fill the room like pine needles covering a fresh cut lawn. Finally, I break down.

“Hello. What are you up to?” I ask.

“You know how yesterday you showed me how to pick out specific lines in a text file. I thought that would work for me, but I guess I mixed up some of the lines in my text files. I need a better way to find things like the telephone numbers in my files,” she said.

“I have a great way to do that very thing. But it will involve extending our lesson on regular expressions from Sunday,” I said.

“What do you mean extend our lesson?” she asked.

“Well on Sunday, I taught you the caret (^) special character, which tells the regular expression to begin matching at the beginning of the line. The best way to match a telephone number is to use another special character— the backslash d,” I said.

“I see,” she said with a bit of reluctance.

“It is easy. Create a variable named a, and then assign a bogus telephone number to it. Then use the match operator to match caret backslash d,” I instructed.

The Scripting Wife thought for a minute, and typed the following commands (<space> is the Space bar, <enter> is the Enter key or Return key).

$a<space>=<space>"123-123-1234"<enter>

$a<space>-match<space>'^\d'<enter>

The following is her command and the associated output.

Image of command output

“So you see, my dear Scripting Wife, that regular expression pattern finds a match with the bogus phone number. Use Select-String to see if it finds Lori’s phone number,” I said.

“OK,” she said.

The Scripting Wife thought for a second or two, and then cautiously typed the following command.

Select-String -Path C:\MyFriends\Lori.txt -Pattern '^\d'

The command and its associated output are shown here.

Image of command output

“Well that almost worked,” she said. “Why do I also have Lori’s street address?”

“That is because the caret backslash d regular expression matches any line that begins with a number. In your Lori.txt file there are two lines that begin with numbers,” I said. The Lori.txt file is shown here.

Image of contact information

“What we need to do now is modify the regular expression pattern so that it will only match three numbers, add a dash and three more numbers, and then add another dash followed by four numbers. To do this, you need to learn how to say three numbers. You put the number 3 inside a set of curly brackets. Go ahead and try that,” I said.

The Scripting Wife thought for a little bit, used the Up arrow to retrieve her previous command, and modified it with {3}. She also added the dashes to separate the different number groups. The command she created appears here.

Select-String -Path C:\MyFriends\Lori.txt -Pattern '^\d{3}-\d{3}-\d{4}'

When she ran the command, the output shown here appeared.

Image of command output

“Wow, did I do that?” the Scripting Wife said, amazed at her progress.

“Indeed you did,” I said, somewhat pleased with both myself and with her.

“It looks really complicated, but it is actually pretty simple in small pieces,” she said. “Thank you.”

And with that she was gone. Who knows where she was headed…

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Ed, you're making regex easy - love it! I've been trying for years to find an easy to understand guide and this is well on the way. Thank you!

  • @Chris

    Thanks! I appreciate that!

  • I 2nd CB's comment.  This RSS feed has become my enjoyable morning read.  If it wasn't for your awesome scripting wife/ps nuggets I may not even consider scripting games.  Thanks for taking the time to provide quality content daily

  • @Gabe I am glad you appreciate the Scripting Wife articles ... she is a fun person. The 2011 Scripting Games will be a lot of fun, coupled with some excellent learning. I hope you are able to enter the games.

  • What if your source(s) contains multiple formats (i.e. "(123) 123-1234", "123-123-1234", "1234567890") and you want them all in the format "123.123.1234" ?