Use PowerShell to Search a Group of Files for Phone Numbers

ScriptingGuy1

Summary: Scripting Wife learns how to use Windows PowerShell to easily search a collection of files for phone numbers.

Spring has nearly sprung in Charlotte, North Carolina. The weather seems to alternate between absolutely perfect to a little cool with rain. Today, at least so far this morning, it is quite pleasant. I am sitting on the back porch watching a couple of squirrels scurry up and down the giant pine trees. I am also sipping a cup of English Breakfast tea, and checking my scripter@microsoft.com email. I really enjoy quiet mornings outside; I find the time useful for contemplating the day, organizing my thoughts, and setting my goals.

“Let’s see, I need to check on the status of my Road to TechEd Live Meetings,” I thought to myself.

“Who are you talking to,” said the Scripting Wife.

“Huh? Where did you come from,” I asked in a startled voice.

“I came from Kentucky; you know that. You know it is not really a good sign when you start talking to yourself,” she said.

“What are you doing up?” I said ignoring her sarcasm.

“I have been up for a while. I have lots of things to do.”

“Not another road trip with your posse,” I said.

“Nope. And they are not a posse,” she said. “Actually, I am working on Windows PowerShell right now. I wanted to ask you a question before you get so busy that you become grumpy.”

“I am more of a Happy than a Grumpy,” I said in an oblique reference to Snow White and the Seven Dwarfs.

“You think I didn’t get your reference to Snow White? With you, it would be more like the Seven Geeks: Dopey, Goofy, Geeky, Cheeky, Cheesy, Silly, and Nerdly.”

“Oh! Oh, that hurts,” I said in mock pain, “You have a strange way of asking for help. Besides, nerds are cool now days.”

“And quit wearing that pocket protector. You didn’t think I would notice?”

“I’m … not …”

“Hah! Made you look,” she said triumphantly.

“All right, you got me,” I admitted. “So what do you want to learn today?”

“Well yesterday, you showed me how to use a regular expression to find telephone numbers in one of my contact text files. That was pretty cool. But after I went back to my computer, I realized that I should have asked you for the next step. Finding a phone number in one file is OK, but I have a whole bunch of contact text files. It would be better if I could search all of them at the same time,” she said adopting a serious look on her face.

“You are right,” I said pausing for effect.

After a few seconds, the Scripting Wife placed her hands on her hips, cocked her head to the left, and said in a rather loud voice, “Well?”

I thought about doing the “What’s the magic word?” thing, but then I was partially afraid of which “magic” word she might adopt, so I relented and said, “Of course my dear. Grab a seat, and I will show you. This is so easy. It is so cool. You will absolutely freak,” I began.

“Cool it. Don’t get all geeky on me,” she said with mock seriousness.

“OK. So you remember the regular expression pattern you used yesterday,” I began.

“It was a slash d something, wasn’t it?” she queried.

“Yepper. Now, what cmdlet did you use,” I asked.

“That is easy…it was the Select-String cmdlet,” she replied.

“Right. Open the Windows PowerShell console, and use the Get-Help cmdlet to retrieve Help on the Select-String cmdlet. A lot of text will scroll off of the window, so you may wish to use the Help function to retrieve the information,” I instructed.

Help function,” she asked, “I thought Help was a cmdlet.”

“Well, the word ‘help’ is a function that includes the ability to display one page of information at a time. It calls the Get-Help cmdlet, and passes information to the More function. The More function is what displays one page at a time,” I explained.

“Uh huh,” she said with a slightly glazed over look on her face. “Are you finished?”

“Yes. Go ahead and use the Help function to retrieve information about the Select-String cmdlet.”

The Scripting Wife thought for a few seconds, and then typed the command that is shown here.

Help Select-String

The command and its associated output appear in the following image.

“Now, what do you notice that is really cool about the Path parameter of Select-String?” I asked.

“Well, uh, nothing.”

“OK, look at the word ‘string’ after the Path parameter. What follows the word ‘string’?” I prompted.

“It has a pair of square brackets after it,” she said.

“Now, do you remember what square brackets mean?” I asked.

“I know what basketball brackets are, but those are not square,” she mocked.

“Right. Square brackets are those symbols that are under the curly brackets and above the Enter key on your keyboard. When you see square brackets it means that the parameter will accept an array.”

“An array is more than one item, right?”

“Yes, you are correct. Therefore, the Path parameter for the Select-String cmdlet will accept more than one path to the file. You know what that means?” I asked.

“Well, I guess it means that I can give Select-String the path to more than one file,” she said basically repeating what I had just stated.

“The best way to illustrate this is to practice using it. Type Select-String in the Windows PowerShell console, and then use Get-ChildItem to retrieve a listing of all your contact files. When you have a listing of your files, use a regular expression pattern to find lines that begin with numbers,” I instructed.

The Scripting Wife looked at me for a second as if attempting to determine from which planet I had just arrived. Then she turned her attention to the Windows PowerShell console. After a few attempts, she looked up proudly. She had created the following command (in the command she uses the gci alias, which a shortcut command for the Get-ChildItem cmdlet).

Select-String -Path (gci C:\MyFriends) -Pattern ‘^\d’

The command and associated output are shown in the following image.

“That is way cool,” she exclaimed.

“Indeed it is,” I said.

“I know where you are going with this. You want me to use the regular expression from yesterday with this command,” she stated.

The Scripting Wife used the Up arrow and retrieved her previous command that retrieved lines that began with numbers. She substituted that pattern, with her phone number pattern from yesterday. The revised command is shown here.

Select-String -Path (gci C:\MyFriends) -Pattern ‘^\d{3}-\d{3}-\d{4}’

The command and its associated output are shown in the following image.

“Oh that is way cool,” she said. “And it was easy. Well I am outta here,” she said.

“I thought you said you were working on Windows PowerShell today,” I queried.

“Well, I thought it would take me all day to write a script to pull out the phone numbers from all of my contact files. We did it in less than 15 minutes, even with your excursions into PowerShell trivia. I have the entire day ahead of me, and I am not going to sit around here. I think I will put that phone list to good use, and organize a day out for my posse. I like that term. It sounds cool,” she said. “Bye-Bye Script Monkey”

And with that, she was gone. She might call me “Script Monkey” sometimes, but I am certain that somehow it is a term of endearment. It must be.

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 

0 comments

Discussion is closed.

Feedback usabilla icon