{"id":1114,"date":"2009-02-03T08:00:09","date_gmt":"2009-02-03T16:00:09","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/02\/03\/february-powershell-one-liner\/"},"modified":"2009-02-03T08:00:09","modified_gmt":"2009-02-03T16:00:09","slug":"february-powershell-one-liner","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/02\/03\/february-powershell-one-liner\/","title":{"rendered":"February PowerShell One-Liner"},"content":{"rendered":"<p>If you subscribe to the SAPIEN newsletter you should have seen this already. If not, why don&#8217;t you take a moment to subscribe? Create an account at <a href=\"http:\/\/my.sapien.com\" target=\"_blank\">my.SAPIEN.com<\/a> and you&#8217;ll automatically be subscribed. Each month you&#8217;ll get all the latest product and training news from SAPIEN Technologies as well as useful tips like this Powershell one-liner that produces a report on event log sizes for multiple computers.<\/p>\n<p>This is a single line PowerShell expression, although you could easily insert it into a script file to save some typing. I&#8217;ve inserted line breaks using the escape character (`) to make this easier to read.<\/p>\n<div style=\"border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4\">\n<div style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">Get-Content c:\\computers.txt  | where {($_.Trim()).length <span style=\"color: #cc6633\">-gt<\/span> 0} | <span style=\"color: #0000ff\">foreach<\/span> { <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"> Get-WmiObject Win32_NTEventLogFile -computer $_.Trim() `<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"> -<span style=\"color: #0000ff\">filter<\/span> <span style=\"color: #006080\">\"NumberOfRecords &gt; 0\"<\/span> | Select-Object `<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"> @{Name=<span style=\"color: #006080\">\"Computername\"<\/span>;Expression={$_.CSName}},LogFileName,NumberOfRecords,`<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"> @{Name=<span style=\"color: #006080\">\"Size(KB)\"<\/span>;Expression={$_.FileSize\/1kb}},`<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"> @{Name=<span style=\"color: #006080\">\"MaxSize(KB)\"<\/span>;Expression={($_.MaxFileSize\/1KB) -as [int]}}, `<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"> @{name=<span style=\"color: #006080\">\"PercentUsed\"<\/span>;Expression={<span style=\"color: #006080\">\"{0:P2}\"<\/span> -f ($_.filesize\/$_.maxFileSize)}}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">} |  Sort Computername  | Format-Table -GroupBy Computername `<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\">-property LogFileName,NumberOfRecords,*Size*,PercentUsed<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">&nbsp;<\/pre>\n<\/div>\n<\/div>\n<p>This expression will go through all the computer names in C:\\computers.txt. Each string is validated by <strong>Where-Object<\/strong> which strips out any surrounding spaces and only sends on strings with a length greater than 0.&nbsp; This weeds out blank lines. Each computer name is then passed to <strong>ForEach-Object<\/strong> which uses<strong> Get-WmiObject<\/strong> to query the Win32_NTEventLogFile, looking for event logs with more than 0 entries. <\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/02\/feb09onelinercapture.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 5px 5px 5px 0px; border-right-width: 0px\" height=\"159\" alt=\"feb09OneLinerCapture\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/02\/feb09onelinercapture-thumb.png\" width=\"244\" align=\"right\" border=\"0\"><\/a>Each event log is then passed to <strong>Select-Object<\/strong> which creates a custom object for the computer, defining properties for name, size and maximum size in KB and a percentage used. These results are piped to <strong>Sort-object<\/strong> which sorts on the <em>computername<\/em> property. Finally output is piped to <strong>Format-Table<\/strong> which groups the output by <em>computername<\/em> and prepares a formatted report. <\/p>\n<p>I ended the expression by creating a report, which you could have further sent to a file using <strong>Out-File<\/strong> or directly to a printer using <strong>Out-Printer<\/strong>. Or perhaps instead of a formatted report, you could have created an HTML report using <strong>ConvertTo-HTML<\/strong>. The point is that PowerShell&#8217;s pipeline is flexible and versatile.<\/p>\n<p>If you are new to PowerShell looking for a resource, take a look at <a href=\"http:\/\/www.sapienpress.com\/PowerShell2.asp\" target=\"_blank\">Windows PowerShell v1.0: TFM 2nd Edition<\/a>. Also keep an eye on the blog for announcements regarding PowerShell training this year. Finally, if you need help with this one-liner or any PowerShell question, please join the discussion at <a href=\"http:\/\/www.scriptinganswers.com\" target=\"_blank\">ScriptingAnswers.com<\/a>.<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:53fe57b0-5973-41eb-8107-d017f6ba5ab3\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<p>If you want to try this one-liner out, download it <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/02\/feb09oneliner.txt\" target=\"_blank\">here.<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you subscribe to the SAPIEN newsletter you should have seen this already. If not, why don&#8217;t you take a moment to subscribe? Each month you&#8217;ll get all the latest product and training news from SAPIEN Technologies as well as useful tips like this Powershell one-liner that produces a report on event log sizes for multiple computers.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,25],"tags":[65,403,249,28,35],"class_list":["post-1114","post","type-post","status-publish","format-standard","hentry","category-howto","category-windows-powershell","tag-automation","tag-eventlog","tag-get-wmiobject","tag-powershell","tag-wmi"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=1114"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1114\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}