{"id":15,"date":"2006-12-15T04:00:00","date_gmt":"2006-12-15T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/15\/more-fun-with-file-aging\/"},"modified":"2006-12-15T04:00:00","modified_gmt":"2006-12-15T12:00:00","slug":"more-fun-with-file-aging","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/15\/more-fun-with-file-aging\/","title":{"rendered":"More Fun with File Aging"},"content":{"rendered":"<p>A few days ago, I posted an <a href=\"http:\/\/sapien.eponym.com\/blog\/_archives\/2006\/12\/14\/2571520.html\">entry <\/a>on how to run a file aging report using PowerShell. The script as written was pretty and all, but a little limiting. You really can&#8217;t do anything with the results. In many regards it is a VBScript rewritten for PowerShell (not that there&#8217;s anything wrong with that). But PowerShell can do so much more, especially when you think about piping objects between cmdlets. The original script just produces string output so you can&#8217;t do much else with it. What the script needs is a way to produce an object that can then be used by other PowerShell cmdlets. Thus was born FileAging2.ps1.&nbsp; I&#8217;ve attached the file as a text file so you don&#8217;t have to copy and paste from the blog entry.<\/p>\n<p>The script is essentially the same as before. (I did update my formatting expressions per Jeffrey Snover&#8217;s suggestion about using 1MB instead of 1048576)<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\"># FileAging2.ps1<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\"># Jeffery Hicks SAPIEN Technologies, Inc.<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\"># jhicks@sapien.com<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\"># http:\/\/www.SAPIEN.com&nbsp; http:\/\/www.ScriptingAnswers.com<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\"># http:\/\/sapien.eponym.com\/blog<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\"># This script developed and tested with PowerShell RTM<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\"># !! TEST IN A NON-PRODUCTION ENVIRONMENT !!<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Param($dir)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">if ($dir -isnot [string]) {<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Host &#8220;ERROR: You must specify a directory path!&#8221; `<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">-foregroundcolor &#8220;RED&#8221; -backgroundcolor &#8220;Black&#8221;;`<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">return<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$now=Get-Date<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$files=Get-ChildItem -path $dir -recurse | `<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">where {($_.GetType()).name -eq &#8220;FileInfo&#8221;}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">clear-host<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">#initialize<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total2yr=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total90=0 <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total1yr=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total30=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total7=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$TotalCurrent=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$2yrs=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$1yr=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$3mo=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$1mo=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$1wk=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$current=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$count=0<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">#enumerate files and get information<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">foreach ($file in $files) {<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp; $age=($now.subtract(($file.LastWriteTime))).days<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp; $count=$count+1<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp; Write-Progress -Activity &#8220;File Aging Report&#8221; `<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp; -status $file.DirectoryName -currentoperation $file.name <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp; switch ($age) {<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; {$age -ge 730} {$2yrs=$2yrs+1;$Total2yr=$Total2Yr+$file.length;break}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; {$age -ge 365} {$1yr=$1yr+1;$Total1yr=$Total1Yr+$file.length;break}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; {$age -ge 90} {$3Mo=$3Mo+1;$Total90=$Total90+$file.length;break} <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; {$age -ge 30} {$1Mo=$1Mo+1;$Total30=$Total30+$file.length;break}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; {$age -ge 7} {$1wk=$1wk+1;$Total7=$Total7+$file.length;break}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; {$age -lt 7}&nbsp; {$current=$current+1;$TotalCurrent=$TotalCurrent+`<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; $file.Length;break}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp; }<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">}<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">#format file size totals to MB<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total2yr=&#8221;{0:N2}&#8221; -f ($Total2yr\/1MB)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total90=&#8221;{0:N2}&#8221; -f ($Total90\/1MB) <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total1yr=&#8221;{0:N2}&#8221; -f ($Total1yr\/1MB)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total30=&#8221;{0:N2}&#8221; -f ($Total30\/1MB)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$Total7=&#8221;{0:N2}&#8221; -f ($Total7\/1MB)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$TotalCurrent=&#8221;{0:N2}&#8221; -f ($TotalCurrent\/1MB)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">#create new object to hold values<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$obj = New-Object System.Object<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Age -value &#8220;2Yrs&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Count -value $2yrs<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name MB -value $Total2yr<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name DirPath -value $dir.ToString() <\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Output $obj<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$obj = New-Object System.Object<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Age -value &#8220;1Yr&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Count -value $1yr<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name MB -value $Total1yr<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name DirPath -value $dir.ToString()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Output $obj<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$obj = New-Object System.Object<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Age -value &#8220;3Mo&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Count -value $3mo<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name MB -value $Total90<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name DirPath -value $dir.ToString()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Output $obj<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$obj = New-Object System.Object<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Age -value &#8220;1Mo&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Count -value $1mo<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name MB -value $Total30<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name DirPath -value $dir.ToString()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Output $obj<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$obj = New-Object System.Object<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Age -value &#8220;1Wk&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Count -value $1wk<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name MB -value $Total7<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name DirPath -value $dir.ToString()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Output $obj<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">$obj = New-Object System.Object<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Age -value &#8220;Curr&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name Count -value $current<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name MB -value $TotalCurrent<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Add-Member -inputobject $obj -membertype NoteProperty -Name DirPath -value $dir.ToString()<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Write-Output $obj<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">#EOF<\/span><\/p>\n<p>The primary improvement to the script is using New-Object to create a generic .NET object and then calling Add-Member to populate the object with data. Write-Object sends the object data to PowerShell so it can present it. But that also means I have an object I can pipe to other cmdlets.<\/p>\n<p>Here&#8217;s a screen shot of what I get when I run a the command:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">.\\fileaging2.ps1 s:\\ | format-table -autosize<\/span><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/sapien.eponym.com\/screenshots\/fileaging2.png\"><\/p>\n<p>Or I could do something like:<br \/><span style=\"font-family: Courier New,Courier,mono;\">.\\fileaging2.ps1 s:\\ | export-csv &#8220;c:\\reports\\S-age.csv&#8221;<\/span><\/p>\n<p>and save the information to a csv file.<\/p>\n<p>I can even take this a step further and use <a href=\"http:\/\/www.powergadgets.com\/\">PowerGadget<\/a>&#8216;s out-chart cmdlet. I can run an expression like this:<br \/><span style=\"font-family: Courier New,Courier,mono;\">.\\fileaging2.ps1 s:\\ | out-chart -values Count,MB -label Age -Title &#8220;File Aging Report: S:\\&#8221; -floating -AllSeries_PointLabels_Visible True<\/span><br \/>&nbsp;<br \/>and get something like this:<br \/><img decoding=\"async\" src=\"http:\/\/sapien.eponym.com\/screenshots\/fileagingchart.png\"><\/p>\n<p>Or suppose I wanted a break down of files by age as a percentage of the total:<br \/><span style=\"font-family: Courier New,Courier,mono;\">.\\fileaging2.ps1 s:\\out-chart -values Count -label Age -gallery pie -AllSeries_PointLabels_Visible True -galleryAttributes_LabelsInside -title &#8220;File Aging Report: S:\\ (by count)&#8221; -floating<\/span><br \/><img decoding=\"async\" src=\"http:\/\/sapien.eponym.com\/screenshots\/fileagingpie.png\"><\/p>\n<p>Once my script starts acting like an object, so many other possibilities present themselves. I&#8217;ve given up the string color coding that the original script had, but I believe &#8220;objectifying&#8221; the script more than covers the &#8220;loss.&#8221;<\/p>\n<p>[Thanks to the folks at PowerGadgets for helping me get on the right track]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few days ago, I posted an entry on how to run a file aging report using PowerShell. The script as written was pretty and all, but a little limiting. You really can&#8217;t do anything with the results. In many regards it is a VBScript rewritten for PowerShell (not that there&#8217;s anything wrong with that). But PowerShell can do so much more, especially when you think about piping objects between cmdlets. The original script just produces string output so you can&#8217;t do much else with it. What the script needs is a way to produce an object that can then be used by other PowerShell cmdlets.<\/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":[2,25],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15","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=15"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}