{"id":636,"date":"2014-09-24T00:01:00","date_gmt":"2014-09-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/24\/use-select-string-cmdlet-in-powershell-to-view-contents-of-log-file\/"},"modified":"2014-09-24T00:01:00","modified_gmt":"2014-09-24T00:01:00","slug":"use-select-string-cmdlet-in-powershell-to-view-contents-of-log-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-select-string-cmdlet-in-powershell-to-view-contents-of-log-file\/","title":{"rendered":"Use Select-String Cmdlet in PowerShell to View Contents of Log File"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Use the Windows PowerShell cmdlet, <\/span><b style=\"font-size:12px\">Select-String<\/b><span style=\"font-size:12px\">, to view the contents of a log file.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have a log file that I created by dumping process information from <b>Get-Process<\/b>. It is quite long, and I am trying to use <b>Select-String<\/b> to find the number of instances of a certain process. The problem is that when I find the instance, I do not know what the columns mean. I am wondering what I can do to get the column headers in addition to the process information to show up in my output. I know I can do this with a script, but I don&rsquo;t want to write a script to accomplish this task. Can you help?<\/p>\n<p>&mdash;BU<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello BU,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well, I am sore again. It seems that when I go see my trainer, I come away sore. After the weekend, I start to feel good, and then BOOM!&mdash;I go back and I am sore all over again. And I do mean sore all over.<\/p>\n<p>Anyway, the Scripting Wife bought me a nice one-pound bag of English Breakfast tea, so I am sitting here, trying to cool down, sipping a cup of English Breakfast tea with a cinnamon stick in it, and thinking about rejoining the world of the living. I am checking my email sent to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, and BU, I ran across your email. The answer is, &quot;Sure, it can be done. In fact, it is not too hard at all.&quot;<\/p>\n<h2>First the log file<\/h2>\n<p>You say that you have a log file you created by using <b>Get-Process<\/b>. I am assuming the command you used was something like the following:<\/p>\n<p style=\"margin-left:30px\">Get-Process &gt;&gt; C:\\FSO|MyProcesses.txt<\/p>\n<p>The resulting log file is shwon here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I use the <b>Select-String<\/b> cmdlet to read the log file and to look for instances related to the <b>iexplore<\/b> process, I might use a command such as this:<\/p>\n<p style=\"margin-left:30px\">Get-Content C:\\fso\\myprocesses.txt | Select-String &#039;iexplore&#039;<\/p>\n<p>The command and the output from the command are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The problem with this command, is that I cannot tell what the columns of numbers mean. Also, it is more work than is required. I do not need to use <b>Get-Content<\/b> first. I can simply use <b>Select-String<\/b> to find the information I need.<\/p>\n<h2>One way to get column headings<\/h2>\n<p>I can use two commands to get the column headings. The first is to use the <b>Get-Content<\/b> cmdlet and return only the first two lines from the file. This will give me the column headings. The command is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Content C:\\fso\\myprocesses.txt -TotalCount 2<\/p>\n<p>I can then use the previous command to display the column details, as shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>It is not ideal, but it does give me an idea of what is going on, and I can line up the column headings well enough to decipher the output. So, this will work.<\/p>\n<h2>Simplify things<\/h2>\n<p>I said that I do not need to resort to <b>Get-Content<\/b> at all. In fact, I can use <b>Select-String<\/b> to parse the file and find the information all at the same time. To do this, all I need to do is to specify the path to the file, for example:<\/p>\n<p style=\"margin-left:30px\">Select-String -Path C:\\fso\\myprocesses.txt -Pattern iexplore<\/p>\n<p>The command and the output are shown here (note that this command includes the file and the line number where the match occurred).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I still need to obtain the headers from the file to be able to make sense of the output. I could go back to using <b>Get-Content<\/b>, but the output would not be quite as readable. A better way is to use the regular expression <b>OR<\/b> pattern. I know that one of the columns includes the word Handles. So I can specify my pattern to be <b>iexplore<\/b> OR <b>Handles<\/b>. Here is the command I use:<\/p>\n<p style=\"margin-left:30px\">Select-String -Path C:\\fso\\myprocesses.txt -Pattern &quot;(iexplore|Handles)&quot;<\/p>\n<p>The command and the output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The output is a little better, and the columns line up pretty well. I may decide to leave it at this. But if I do not need the <b>Line number<\/b> field or the <b>Path<\/b> field, I can clean up the output. To do this, I need to know the properties of the <b>MatchInfo<\/b> object. I get these from <b>Get-Member<\/b> as shown here:<\/p>\n<p style=\"margin-left:30px\">Select-String -Path C:\\fso\\myprocesses.txt -Pattern &quot;(iexplore|Handles)&quot; | get-member -MemberType Properties<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; TypeName: Microsoft.PowerShell.Commands.MatchInfo<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">Context&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}<\/p>\n<p style=\"margin-left:30px\">Filename&nbsp;&nbsp; Property&nbsp;&nbsp; string Filename {get;}<\/p>\n<p style=\"margin-left:30px\">IgnoreCase Property&nbsp;&nbsp; bool IgnoreCase {get;set;}<\/p>\n<p style=\"margin-left:30px\">Line&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string Line {get;set;}<\/p>\n<p style=\"margin-left:30px\">LineNumber Property&nbsp;&nbsp; int LineNumber {get;set;}<\/p>\n<p style=\"margin-left:30px\">Matches&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Text.RegularExpressions.Match[] Matches {get;set;}<\/p>\n<p style=\"margin-left:30px\">Path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string Path {get;set;}<\/p>\n<p style=\"margin-left:30px\">Pattern&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string Pattern {get;set;}<\/p>\n<p>Luckily, the property names make sense. Obviously, <b>Pattern<\/b> is the pattern I specified to find the matches. The <b>LineNumber<\/b> and <b>Path<\/b> properties are the file and the line number in the file. So I want the <b>Line<\/b> property. Here is my revised command:<\/p>\n<p style=\"margin-left:30px\">Select-String -Path C:\\fso\\myprocesses.txt -Pattern &quot;(iexplore|Handles)&quot; | select line<\/p>\n<p>Here is the command and the revised output. It is quite readable now.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-06.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-24-14-06.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>BU, that is all there is to using <b>Select-String<\/b>. Join me tomorrow when I will talk about more way cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use the Windows PowerShell cmdlet, Select-String, to view the contents of a log file. &nbsp;Hey, Scripting Guy! I have a log file that I created by dumping process information from Get-Process. It is quite long, and I am trying to use Select-String to find the number of instances of a certain process. The problem [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,336,45],"class_list":["post-636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-strings","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use the Windows PowerShell cmdlet, Select-String, to view the contents of a log file. &nbsp;Hey, Scripting Guy! I have a log file that I created by dumping process information from Get-Process. It is quite long, and I am trying to use Select-String to find the number of instances of a certain process. The problem [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/636","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=636"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/636\/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=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}