Based on files LastWriteTime property my script create a table 'File Age' with a custom output. The script work fine, my problem is with the output of a specific column 'Age' that i'cant set the order like i will. I already try to use cmdlet "Sort-Object -descending" but without success,data is randomly positioned. Maybe there is another way to customize that?
My desired output look like that:
Age
1 Day - 1 Week
1 Week - 1 Month
1 Month - 6 Months
6 Months - 1 Year
1 Year - 2 Years
< 2 Years
Here is my script:
$scan = gci c:\MyFolder
$scan | Add-Member ScriptProperty -Name FileAgeDays -Value {
[int]((Get-Date) - ($this.LastWriteTime)).TotalDays}
$scan | Add-Member ScriptProperty -Name FileAge -Value {
if ($this.FileAgeDays -le 7) {
"1 Day - 1 Week"
}
elseif ($this.FileAgeDays -ge 7 -and $this.FileAgeDays -le 30) {
"1 Week - 1 Month"
}
elseif ($this.FileAgeDays -ge 30 -and $this.FileAgeDays -le 180) {
"1 Month - 6 Months"
}
elseif ($this.FileAgeDays -ge 180 -and $this.FileAgeDays -le 365) {
"6 Months - 1 Year"
}
elseif ($this.FileAgeDays -ge 365 -and $this.FileAgeDays -le 730) {
"1 Year - 2 Years"
}
elseif ($this.FileAgeDays -ge 730) {
"< 2 Years"
}
}
$grp = $scan | ?{ ! $_.PSIsContainer } | Group FileAge |
Add-Member -MemberType ScriptProperty -Name SizeMB -Value {
($this.Group | Measure-Object Length -sum).Sum / 1MB} -PassThru
$age_frag = $grp | select`
@{l='Size (MB)';e={"{0:n}" -f ($_.sizemb)}},`
@{l='Age';e={$_.name}}, Count|
ConvertTo-Html -Fragment -As Table -PreContent "<br><H2><a name=a009>File Age</a></H2>"|
Out-String
Any help will be welcome.