{"id":15151,"date":"2011-03-24T00:01:00","date_gmt":"2011-03-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/03\/24\/use-powershell-to-search-a-group-of-files-for-phone-numbers\/"},"modified":"2011-03-24T00:01:00","modified_gmt":"2011-03-24T00:01:00","slug":"use-powershell-to-search-a-group-of-files-for-phone-numbers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-search-a-group-of-files-for-phone-numbers\/","title":{"rendered":"Use PowerShell to Search a Group of Files for Phone Numbers"},"content":{"rendered":"<p><strong><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">Summary<\/span><\/strong><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">: Scripting Wife learns how to use Windows PowerShell to easily search a collection of files for phone numbers.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">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 <a target=\"_blank\" href=\"mailto:Scripter@Microsoft.Com\"><span style=\"color: #0000ff\">scripter@microsoft.com<\/span><\/a> email. I really enjoy quiet mornings outside; I find the time useful for contemplating the day, organizing my thoughts, and setting my goals. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Let&rsquo;s see, I need to check on the status of my Road to TechEd Live Meetings,&rdquo; I thought to myself. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Who are you talking to,&rdquo; said the Scripting Wife.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Huh? Where did you come from,&rdquo; I asked in a startled voice.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;I came from Kentucky; you know that. You know it is not really a good sign when you start talking to yourself,&rdquo; she said. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;What are you doing up?&rdquo; I said ignoring her sarcasm. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;I have been up for a while. I have lots of things to do.&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Not another road trip with your posse,&rdquo; I said.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Nope. And they are not a posse,&rdquo; she said. &ldquo;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.&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;I am more of a Happy than a Grumpy,&rdquo; I said in an oblique reference to Snow White and the Seven Dwarfs. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;You think I didn&rsquo;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.&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Oh! Oh, that hurts,&rdquo; I said in mock pain, &ldquo;You have a strange way of asking for help. Besides, nerds are cool now days.&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;And quit wearing that pocket protector. You didn&rsquo;t think I would notice?&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;I&rsquo;m &hellip; not &hellip;&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Hah! Made you look,&rdquo; she said triumphantly. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;All right, you got me,&rdquo; I admitted. &ldquo;So what do you want to learn today?&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Well <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/03\/23\/use-powershell-to-look-for-phone-numbers-in-text.aspx\">yesterday<\/a>, 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,&rdquo; she said adopting a serious look on her face.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;You are right,&rdquo; I said pausing for effect.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">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, &ldquo;Well?&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">I thought about doing the &ldquo;What&rsquo;s the magic word?&rdquo; thing, but then I was partially afraid of which &ldquo;magic&rdquo; word she might adopt, so I relented and said, &ldquo;Of course my dear. Grab a seat, and I will show you. This is so easy. It is so cool. You will absolutely freak,&rdquo; I began.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Cool it. Don&rsquo;t get all geeky on me,&rdquo; she said with mock seriousness. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;OK. So you remember the regular expression pattern you used yesterday,&rdquo; I began.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;It was a <b>slash d something<\/b>, wasn&rsquo;t it?&rdquo; she queried.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Yepper. Now, what cmdlet did you use,&rdquo; I asked.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;That is easy&hellip;it was the <b>Select-String<\/b> cmdlet,&rdquo; she replied. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Right. Open the Windows PowerShell console, and use the <b>Get-Help<\/b> cmdlet to retrieve Help on the <b>Select-String<\/b> cmdlet. A lot of text will scroll off of the window, so you may wish to use the <b>Help<\/b> function to retrieve the information,&rdquo; I instructed. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;<b>Help<\/b> function,&rdquo; she asked, &ldquo;I thought Help was a cmdlet.&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Well, the word &lsquo;<span>help<\/span>&rsquo; is a function that includes the ability to display one page of information at a time. It calls the <b>Get-Help<\/b> cmdlet, and passes information to the <b>More<\/b> function. The <b>More<\/b> function is what displays one page at a time,&rdquo; I explained.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Uh huh,&rdquo; she said with a slightly glazed over look on her face. &ldquo;Are you finished?&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Yes. Go ahead and use the <b>Help<\/b> function to retrieve information about the <b>Select-String<\/b> cmdlet.&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\"><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">The Scripting Wife thought for a few seconds, and then typed the command that is shown here.<\/span><\/p>\n<p style=\"text-indent: 0.5in\"><span style=\"font-family: 'Lucida Sans Typewriter';color: black;font-size: 10pt\">Help Select-String<\/span><\/p>\n<p><span style=\"line-height: 115%;font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">The command and its associated output appear in the following image.<\/span><\/p>\n<p><\/span><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4442.1.jpg\" border=\"0\" \/><\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\"><\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Now, what do you notice that is really cool about the <i>Path <\/i>parameter of <b>Select-String<\/b>?&rdquo; I asked.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Well, uh, nothing.&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;OK, look at the word &lsquo;<span>string<\/span>&rsquo; after the <i>Path<\/i> parameter. What follows the word &lsquo;<span>string&rsquo;<\/span>?&rdquo; I prompted.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;It has a pair of square brackets after it,&rdquo; she said. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Now, do you remember what square brackets mean?&rdquo; I asked.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;I know what basketball brackets are, but those are not square,&rdquo; she mocked.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;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.&rdquo; <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;An array is more than one item, right?&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Yes, you are correct. Therefore, the <i>Path<\/i> parameter for the <b>Select-String<\/b> cmdlet will accept more than one path to the file. You know what that means?&rdquo; I asked.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Well, I guess it means that I can give <b>Select-String<\/b> the path to more than one file,&rdquo; she said basically repeating what I had just stated.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;The best way to illustrate this is to practice using it. Type <b>Select-String<\/b> in the Windows PowerShell console, and then use <b>Get-ChildItem<\/b> 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,&rdquo; I instructed. <\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\"><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">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 <b>gci<\/b> alias, which a shortcut command for the <b>Get-ChildItem<\/b> cmdlet).<\/span><\/p>\n<p style=\"text-indent: 0.5in\"><span style=\"font-family: 'Lucida Sans Typewriter';color: black;font-size: 10pt\">Select-String -Path (gci C:\\MyFriends) -Pattern &#8216;^\\d&#8217;<\/span><\/p>\n<p><span style=\"line-height: 115%;font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">The command and associated output are shown in the following image.<\/span><\/p>\n<p><\/span><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8475.2.jpg\" border=\"0\" \/><\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;That is way cool,&rdquo; she exclaimed.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Indeed it is,&rdquo; I said.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;I know where you are going with this. You want me to use the regular expression from yesterday with this command,&rdquo; she stated.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\"><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">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.<\/span><\/p>\n<p style=\"text-indent: 0.5in\"><span style=\"font-family: 'Lucida Sans Typewriter';color: black;font-size: 10pt\">Select-String -Path (gci C:\\MyFriends) -Pattern &#8216;^\\d{3}-\\d{3}-\\d{4}&#8217;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">The command and its associated output are shown in the following image.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5810.3.jpg\" border=\"0\" \/><\/span><\/p>\n<p><\/span><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\"><\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;Oh that is way cool,&rdquo; she said. &ldquo;And it was easy. Well I am outta here,&rdquo; she said.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;I thought you said you were working on Windows PowerShell today,&rdquo; I queried.<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">&ldquo;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,&rdquo; she said. &ldquo;Bye-Bye Script Monkey&rdquo;<\/span><\/p>\n<p><span style=\"font-family: 'Arial','sans-serif';color: black;font-size: 10pt\">And with that, she was gone. She might call me &ldquo;Script Monkey&rdquo; sometimes, but I am certain that somehow it is a term of endearment. It must be. <\/span><\/p>\n<p><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\">I invite you to follow me on <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\"><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\"><span style=\"color: #0000ff\">Twitter<\/span><\/span><\/a><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\"> and <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\"><span style=\"color: #0000ff\">Facebook<\/span><\/span><\/a><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\">. If you have any questions, send email to me at <\/span><a target=\"_blank\" href=\"mailto:scripter@microsoft.com\"><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\"><span style=\"color: #0000ff\">scripter@microsoft.com<\/span><\/span><\/a><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\">, or post your questions on the <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\"><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\"><span style=\"color: #0000ff\">Official Scripting Guys Forum<\/span><\/span><\/a><span style=\"font-family: 'Segoe','sans-serif';font-size: 10pt\">. See you tomorrow. Until then, peace.<\/span><\/p>\n<p><b><span style=\"font-family: 'Segoe UI','sans-serif';font-size: 10pt\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><span style=\"line-height: 115%;font-size: 10pt\"><span style=\"font-family: Calibri\">&nbsp;<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,174,3,4,74,45],"class_list":["post-15151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-regular-expressions","tag-scripting-guy","tag-scripting-techniques","tag-scripting-wife","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15151"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15151\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}