{"id":4117,"date":"2013-02-24T00:01:00","date_gmt":"2013-02-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/02\/24\/weekend-scripter-two-way-cool-powershell-text-file-tricks-tail-and-wait\/"},"modified":"2013-02-24T00:01:00","modified_gmt":"2013-02-24T00:01:00","slug":"weekend-scripter-two-way-cool-powershell-text-file-tricks-tail-and-wait","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-two-way-cool-powershell-text-file-tricks-tail-and-wait\/","title":{"rendered":"Weekend Scripter: Two Way-Cool PowerShell Text File Tricks: Tail and Wait"},"content":{"rendered":"<p><span style=\"font-size: 12px\"><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, talks about two way-cool new Windows PowerShell parameters: Tail and Wait.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Some things in Windows PowerShell are just so cool that I get all carried away with them and lose focus on whatever I am supposed to be doing. It happens that way sometimes. Luckily, it is the weekend, so I am covered for frittering away my time. But hey, it is for a good cause.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;Today I talk about reading from a log file as it updates. I also talk about reading from the end of a log file. I am using the script I wrote yesterday to add to the log file as I watch for updates. See yesterday&rsquo;s article, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/02\/23\/weekend-scripter-creating-a-sample-log-file-by-using-powershell.aspx\" target=\"_blank\">Weekend Scripter: Creating a Sample Log File by Using PowerShell<\/a>,<em> <\/em>for that script and for a discussion of the script.<\/p>\n<h2>Reading x number of lines from beginning or from end of log<\/h2>\n<p>For log files that log to the end of the file, the most recent information is at the bottom of the file. This can be a problem if I don&rsquo;t know how many lines of stuff are in the log. Oh, I can do it, but even using Windows PowerShell, it requires a bit of code. Not anymore, because we now have the <strong>tail<\/strong><em> <\/em>parameter that will retrieve a specific number of lines from the very bottom of the file. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Content -Path C:\\fso\\mylogfile.log -Tail 1<\/p>\n<p style=\"padding-left: 30px\">Added sample 125 at 4:0:42<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Content -Path C:\\fso\\mylogfile.log -Tail 2<\/p>\n<p style=\"padding-left: 30px\">Added sample 123 at 4:0:42<\/p>\n<p style=\"padding-left: 30px\">Added sample 125 at 4:0:42<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Content -Path C:\\fso\\mylogfile.log -Tail 3<\/p>\n<p style=\"padding-left: 30px\">Added sample 121 at 4:0:42<\/p>\n<p style=\"padding-left: 30px\">Added sample 123 at 4:0:42<\/p>\n<p style=\"padding-left: 30px\">Added sample 125 at 4:0:42<\/p>\n<p>If I am interested in the first lines in the file, I use the <strong>&ndash;head<\/strong><em> <\/em>parameter and specify how many lines I want to obtain. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Content -Path C:\\fso\\mylogfile.log -Head 2<\/p>\n<p style=\"padding-left: 30px\">Added sample 0 at 3:46:40<\/p>\n<p style=\"padding-left: 30px\">Added sample 1 at 3:46:40<\/p>\n<h2>But what about when the action is still occurring?<\/h2>\n<p>If the action is still occurring, that is, it is still being written to the log file, use the <strong>&ndash;wait<\/strong><em> <\/em>parameter of <strong>Get-Content<\/strong>. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Content -Path C:\\fso\\mylogfile.log -Tail 1 &ndash;Wait<\/p>\n<p>In this way, I retrieve the last written log file entry as it occurs.<\/p>\n<p>To test this, I run the command above in the Windows PowerShell console. The Windows PowerShell console waits for new lines to be written to the file. I then switch to the Windows PowerShell ISE and run my Add-LogV2.ps1 script so that new entries are added to the file.<\/p>\n<p>I switch back to the Windows PowerShell console and watch the entries add new lines to the Windows PowerShell console. The result is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1460.HSG-2-24-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1460.HSG-2-24-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Well, that is about it for monitoring a log file. Join me tomorrow when I will talk about parsing the System event log.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about two way-cool new Windows PowerShell parameters: Tail and Wait. Microsoft Scripting Guy, Ed Wilson, is here. Some things in Windows PowerShell are just so cool that I get all carried away with them and lose focus on whatever I am supposed to be doing. It happens that [&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":[362,3,4,14,61,45],"class_list":["post-4117","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell-3","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about two way-cool new Windows PowerShell parameters: Tail and Wait. Microsoft Scripting Guy, Ed Wilson, is here. Some things in Windows PowerShell are just so cool that I get all carried away with them and lose focus on whatever I am supposed to be doing. It happens that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4117","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=4117"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4117\/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=4117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}