{"id":13,"date":"2006-12-14T04:00:00","date_gmt":"2006-12-14T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/14\/file-aging-with-powershell\/"},"modified":"2018-05-21T10:06:23","modified_gmt":"2018-05-21T17:06:23","slug":"file-aging-with-powershell","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/14\/file-aging-with-powershell\/","title":{"rendered":"File Aging With PowerShell"},"content":{"rendered":"<p>A very common solution that screams for an automated solution is to get a file aging report:\u00a0 Given a specific folder, how many files are there and how old are they?\u00a0 Here is one possible solution that uses PowerShell. It doesn&#8217;t require WMI or the VBScript FileSystemObject, only native PowerShell. The script only requires that you specify a folder name or path to check. This can be a local folder or a UNC. When finished you get a simple, but colorized, report.<\/p>\n<p>I&#8217;ve attached the script for your convenience because sometimes copying and pasting from a web page doesn&#8217;t provide the proper formatting. Rename the file with a .ps1 extension to run.\u00a0 It is not signed so you&#8217;ll have to test it in a non-production environment where code signing is set to unrestricted or sign it with your certificate before running.<\/p>\n<p>Let me show you the script first and then I&#8217;ll go over a few elements.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\"># FileAging.ps1<\/span><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;\">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;\">#verify $Dir exists as a path<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">if (Test-Path $Dir) {<\/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;\">\u00a0\u00a0\u00a0 $now=Get-Date<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 #only get file objects, not folder objects<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $files=Get-ChildItem -path $dir -recurse | `<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 where {($_.GetType()).name -eq &#8220;FileInfo&#8221;}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 clear-host<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 #initialize<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total2yr=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total90=0 <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total1yr=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total30=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total7=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $TotalCurrent=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $2yrs=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $1yr=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $3mo=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $1mo=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $1wk=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $current=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $count=0<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 #enumerate files and get information<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 foreach ($file in $files) {<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $age=($now.subtract(($file.LastWriteTime))).days<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $count=$count+1<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 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;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -status $file.DirectoryName -currentoperation $file.name <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 switch ($age) {<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -ge 730} {$2yrs=$2yrs+1;$Total2yr=$Total2Yr+$file.length;`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -ge 365} {$1yr=$1yr+1;$Total1yr=$Total1Yr+$file.length;`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -ge 90} {$3Mo=$3Mo+1;$Total90=$Total90+$file.length;`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break} <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -ge 30} {$1Mo=$1Mo+1;$Total30=$Total30+$file.length;`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$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;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -lt 7}\u00a0 {$current=$current+1;$TotalCurrent=`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $TotalCurrent+$file.Length;break}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 }<\/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;\">\u00a0\u00a0\u00a0 $GrandTotal=$Total2yr+$Total1yr+$Total90+$Total30+$Total7+$TotalCurrent<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 #format file size totals to MB<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $GrandTotal=&#8221;{0:N2}&#8221; -f ($GrandTotal\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total2yr=&#8221;{0:N2}&#8221; -f ($Total2yr\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total90=&#8221;{0:N2}&#8221; -f ($Total90\/1048576) <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total1yr=&#8221;{0:N2}&#8221; -f ($Total1yr\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total30=&#8221;{0:N2}&#8221; -f ($Total30\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total7=&#8221;{0:N2}&#8221; -f ($Total7\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $TotalCurrent=&#8221;{0:N2}&#8221; -f ($TotalCurrent\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 clear-host<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;File Aging for&#8221; $dir.ToUpper()<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;2 years:&#8221; $2yrs &#8220;files&#8221; $Total2yr &#8220;MB&#8221; -foregroundcolor &#8220;Red&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;1 year:&#8221; $1yr &#8220;files&#8221; $Total1yr &#8220;MB&#8221; -foregroundcolor &#8220;Magenta&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;3 months:&#8221; $3mo &#8220;files&#8221; $Total90 &#8220;MB&#8221; -foregroundcolor &#8220;Yellow&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;1 month:&#8221; $1mo &#8220;files&#8221; $Total30 &#8220;MB&#8221; -foregroundcolor &#8220;Cyan&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;1 week:&#8221; $1wk &#8220;files&#8221; $Total7 &#8220;MB&#8221; -foregroundcolor &#8220;Green&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;Current:&#8221; $current &#8220;files&#8221; $TotalCurrent &#8220;MB&#8221; <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host `n<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;Totals:&#8221; $count &#8220;files&#8221; $GrandTotal &#8220;MB&#8221; <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host `n<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0 }<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0 else {<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0 Write-Host &#8220;Failed to find&#8221; $Dir.ToUpper() -foregroundcolor &#8220;Red&#8221; `<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0 -backgroundcolor &#8220;Black&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0 }<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><br \/>\nThe script starts expecting a run time parameter that will be called $dir. If you forget to specify something, I&#8217;ve added and If statement to see if you entered a string.\u00a0 If not, the script produces a simple error message. I could have used Throw to generate an error if $dir was not defined, but PowerShell includes detailed error information that personally I found annoying and unnecessary, at least for my purposes.<\/p>\n<p><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><\/p>\n<p>If $dir is present, I still want to make sure you entered a path, so I call Test-Path to validate the parameter. If it is a path, then the main body of the script is run.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">if (Test-Path $Dir)<\/span><\/p>\n<p>Otherwise an error message is displayed and the script ends:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">else {<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0 Write-Host &#8220;Failed to find&#8221; $Dir.ToUpper() -foregroundcolor &#8220;Red&#8221; `<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0 -backgroundcolor &#8220;Black&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0 }<\/span><\/p>\n<p>Since this is an aging report, I want to calculate how much time has elapsed from right now so I create a variable, $now, that I can use later in my calculations. Next I call Get-ChildItem with recurse to get every file.\u00a0 I use a Where expression to skip any child items that are folders.<\/p>\n<p><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;\">\u00a0where {($_.GetType()).name -eq &#8220;FileInfo&#8221;}<\/span><\/p>\n<p>Once I have my collection of file information, I can process it and do all of my calculations.\u00a0 This is done with the ForEach construct. I calculate the age of each file by calling the Subtract method of $Now, subtracting it from the LastWriteTime property of each file. I&#8217;m setting this in days.\u00a0 I also keep count of the number of files processed.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $age=($now.subtract(($file.LastWriteTime))).days<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">$count=$count+1<\/span><\/p>\n<p>In order to provide some feedback, I&#8217;m using Write-Progress to report on, well, progress. The cmdlet requires an Activity and Status parameters. Think of them as title bars. The status is updated to reflect the current directory and the CurrentOperation property is set to display the current file name.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">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;\">-status $file.DirectoryName -currentoperation `<br \/>\n$file.name <\/span><\/p>\n<p>The meat of the script is the Switch statement. Instead of checking if $age is a specific number, I can use a calculated expression. So, if the $age is &gt;= 730 days then I update my counters that are keeping track of the number of files that meet this criteria as well as a running total of the total size.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">switch ($age) {<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -ge 730} {$2yrs=$2yrs+1;$Total2yr=$Total2Yr+$file.length;`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -ge 365} {$1yr=$1yr+1;$Total1yr=$Total1Yr+$file.length;`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -ge 90} {$3mo=$3Mo+1;$Total90=$Total90+$file.length;`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break} <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -ge 30} {$1Mo=$1Mo+1;$Total30=$Total30+$file.length;`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$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;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {$age -lt 7}\u00a0 {$current=$current+1;$TotalCurrent=`<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $TotalCurrent+$file.Length;break}<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><br \/>\nIn order to pretty up the output, I spend a moment re-formatting the file size totals so that they come out in MB to a reasonable number of decimal places.<\/p>\n<p><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;\">\u00a0\u00a0\u00a0 $GrandTotal=&#8221;{0:N2}&#8221; -f ($GrandTotal\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total2yr=&#8221;{0:N2}&#8221; -f ($Total2yr\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total90=&#8221;{0:N2}&#8221; -f ($Total90\/1048576) <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total1yr=&#8221;{0:N2}&#8221; -f ($Total1yr\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total30=&#8221;{0:N2}&#8221; -f ($Total30\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $Total7=&#8221;{0:N2}&#8221; -f ($Total7\/1048576)<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 $TotalCurrent=&#8221;{0:N2}&#8221; -f ($TotalCurrent\/1048576)<\/span><\/p>\n<p>Finally, I use Write-Host to display the information with some helpful color coding.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">Write-Host &#8220;File Aging for&#8221; $dir.ToUpper()<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;2 years:&#8221; $2yrs &#8220;files&#8221; $Total2yr &#8220;MB&#8221; -foregroundcolor &#8220;Red&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;1 year:&#8221; $1yr &#8220;files&#8221; $Total1yr &#8220;MB&#8221; -foregroundcolor &#8220;Magenta&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;3 months:&#8221; $3mo &#8220;files&#8221; $Total90 &#8220;MB&#8221; -foregroundcolor &#8220;Yellow&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;1 month:&#8221; $1mo &#8220;files&#8221; $Total30 &#8220;MB&#8221; -foregroundcolor &#8220;Cyan&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;1 week:&#8221; $1wk &#8220;files&#8221; $Total7 &#8220;MB&#8221; -foregroundcolor &#8220;Green&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;Current:&#8221; $current &#8220;files&#8221; $TotalCurrent &#8220;MB&#8221; <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host `n<\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host &#8220;Totals:&#8221; $count &#8220;files&#8221; $GrandTotal &#8220;MB&#8221; <\/span><br style=\"font-family: Courier New,Courier,mono;\" \/><span style=\"font-family: Courier New,Courier,mono;\">\u00a0\u00a0\u00a0 Write-Host `n<\/span><\/p>\n<p>To run the script in PowerShell simply run <span style=\"font-family: Courier New,Courier,mono;\">.\\FileAging.ps1 <\/span><span style=\"font-style: italic; font-family: Courier New,Courier,mono;\">foldername<\/span><\/p>\n<p>In the near future, I&#8217;ll return to this script and show you how to take it to the next level.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A very common solution that screams for an automated solution is to get a file aging report:  Given a specific folder, how many files are there and how old are they?  Here is one possible solution that uses PowerShell. It doesn&#8217;t require WMI or the VBScript FileSystemObject, only native PowerShell. The script only requires that you specify a folder name or path to check. This can be a local folder or a UNC. When finished you get a simple, but colorized, report.<\/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-13","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\/13","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=13"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":15008,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions\/15008"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}