{"id":1175,"date":"2009-03-03T01:00:51","date_gmt":"2009-03-03T09:00:51","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/03\/03\/march-powershell-oneliner\/"},"modified":"2009-02-26T07:48:14","modified_gmt":"2009-02-26T15:48:14","slug":"march-powershell-oneliner","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/03\/03\/march-powershell-oneliner\/","title":{"rendered":"March PowerShell Oneliner"},"content":{"rendered":"<p>In this month&#8217;s SAPIEN newsletter I offered a PowerShell oneliner to returns usage information for a specified folder. I don&#8217;t have room in the newsletter to go into detail so here is more in-depth coverage of this oneliner. I&#8217;ll also have a downlink link at the end.<\/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\">read-host <span style=\"color: #006080\">\"Enter a folder path\"<\/span> -ov fldr | dir -recurse -force `<\/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\">-ea <span style=\"color: #006080\">\"SilentlyContinue\"<\/span> | measure-object length -sum -max -average |<\/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\">Select-Object @{name=<span style=\"color: #006080\">\"Folder\"<\/span>;Expression={$fldr}},`<\/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\">\"Total Files\"<\/span>;Expression={$_.count}},`<\/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\">\"Largest File(MB)\"<\/span>;Expression={<span style=\"color: #006080\">\"{0:F2}\"<\/span> -f ($_.maximum\/1MB)}},`<\/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\">\"Average Size(MB)\"<\/span>;Expression={<span style=\"color: #006080\">\"{0:F2}\"<\/span> -f ($_.average\/1MB)}},`<\/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\">\"Total Size(MB)\"<\/span>;Expression={<span style=\"color: #006080\">\"{0:F2}\"<\/span> -f ($_.sum\/1MB)}}<\/pre>\n<\/div>\n<\/div>\n<p>The oneliner starts out using <strong>Read-Host<\/strong> which prompts you for a directory path. Enter a path with out any quotes, even if it has spaces in the name. You&#8217;ll also notice I&#8217;m using the common parameter -OutVariable, which has an alias of <em>ov<\/em>. I need to save the path name for later.<\/p>\n<p>The path name is piped to <strong>Get-Childitem<\/strong>, I&#8217;m using the DIR alias. I&#8217;m doing a recursive operation using the -force parameter to include hidden files and folders. You&#8217;ll also notice I&#8217;m using another common parameter -ErrorAction which has a convenient alias of <em>ea<\/em>. This parameter instructs the cmdlet on what to do when an error occurs. In my case, I don&#8217;t want to see any error messages or stop.&nbsp; The SilentlyContinue value instructs the cmdlet to keep going, essentially ignoring any errors. On Vista there are a number of folders that are reparse points that will raise errors when accessed from the shell and I don&#8217;t care to see these errors.<\/p>\n<p>The output is then piped to the <strong>Measure-Object<\/strong> cmdlet. I want to return some statistics based on the file size, or length property. I instruct the cmdlet to return the sum, maximum value and average value based on the length property.<\/p>\n<p>Remember, at this point, the file objects are no longer in the pipeline. Instead <strong>Measure-Object<\/strong> is sending out a measurement object which you can verify by piping&nbsp; <strong>Measure-Object<\/strong> expression to <strong>Get-Member<\/strong>. I use <strong>Select-Object<\/strong> and create custom properties using hash tables. The first property is for the folder name. The value is pulled from the variable I used for -OutVariable at the beginning of the expression. The &#8220;Total Files&#8221; property is the Count value from the measurement object. The largest file value is the maximum property formatted to display as MB. I do the same thing for average and sum. <\/p>\n<p>Running the expression will result in output like this:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">Folder&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : c:\\users\\jeff\\documents<br \/>Total Files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2244<br \/>Largest File(MB) : 18.75<br \/>Average Size(MB) : 0.23<br \/>Total Size(MB)&nbsp;&nbsp; : 521.78<\/font> <\/p>\n<p>I could have used one of the Format cmdlets to create the custom object, but I like using <strong>Select-Object<\/strong> as it offers more flexibility. I can export to a CSV file, create an HTML report or create a formatted table with autosized columns.<\/p>\n<p>I would even take this a step further and save this entire one liner as a script block.<\/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\">$usage={read-host <span style=\"color: #006080\">\"Enter a folder path\"<\/span> -ov fldr | <\/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\">dir -recurse -force -ea <span style=\"color: #006080\">\"SilentlyContinue\"<\/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: white; border-bottom-style: none\">measure-object length -sum -max -average |<\/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\">Select-Object @{name=<span style=\"color: #006080\">\"Folder\"<\/span>;Expression={$fldr}},`<\/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\">\"Total Files\"<\/span>;Expression={$_.count}},`<\/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\">\"Largest File(MB)\"<\/span>;Expression={<span style=\"color: #006080\">\"{0:F2}\"<\/span> -f ($_.maximum\/1MB)}},`<\/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\">\"Average Size(MB)\"<\/span>;Expression={<span style=\"color: #006080\">\"{0:F2}\"<\/span> -f ($_.average\/1MB)}},`<\/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\">\"Total Size(MB)\"<\/span>;Expression={<span style=\"color: #006080\">\"{0:F2}\"<\/span> -f ($_.sum\/1MB)}}<\/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\">}<\/pre>\n<\/div>\n<\/div>\n<p>Now all I have to do is invoke the script block.<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" size=\"2\">PS C:\\&gt; &amp;$usage<\/font><\/p>\n<p>The oneliner should work for any directory path you can use with <strong>Get-ChildItem<\/strong>.<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:04be678f-9c97-49bd-8523-fd734619159a\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<p>Download a text file with this oneliner <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/02\/march09-oneliner1.txt\" target=\"_blank\">here.<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this month&#8217;s SAPIEN newsletter I offered a PowerShell oneliner to returns usage information for a specified folder. I don&#8217;t have room in the newsletter to go into detail so here is more in-depth coverage of this oneliner. I&#8217;ll also have a downlink link at the end.<\/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":[25],"tags":[232,460,458,28,457,447,151,383,459],"class_list":["post-1175","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-measure-object","tag-oneliner","tag-outvariable","tag-powershell","tag-read-host","tag-script","tag-scriptblock","tag-select","tag-usage"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1175","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=1175"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"predecessor-version":[{"id":1177,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1175\/revisions\/1177"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}