Skip to content

Instantly share code, notes, and snippets.

@pstakuu
Created August 12, 2022 13:59
Show Gist options
  • Select an option

  • Save pstakuu/e25fa725172342a5402a03de6633688f to your computer and use it in GitHub Desktop.

Select an option

Save pstakuu/e25fa725172342a5402a03de6633688f to your computer and use it in GitHub Desktop.
searches directories and outputs file ages by various time
# FileAging.ps1
# This script developed and tested with PowerShell RTM
Param($dir)
if ($dir -isnot [string]) {
Write-Host “ERROR: You must specify a directory path!” `
-foregroundcolor “RED” -backgroundcolor “Black”;`
return
}
#verify $Dir exists as a path
if (Test-Path $Dir) {
$now=Get-Date
#only get file objects, not folder objects
$files=Get-ChildItem -path $dir -recurse | `
where {($_.GetType()).name -eq “FileInfo”}
#clear-host
#initialize
$Total2yr=0
$Total90=0
$Total1yr=0
$Total30=0
$Total7=0
$TotalCurrent=0
$2yrs=0
$1yr=0
$3mo=0
$1mo=0
$1wk=0
$current=0
$count=0
#enumerate files and get information
foreach ($file in $files) {
$age=($now.subtract(($file.LastWriteTime))).days
$count=$count+1
Write-Progress -Activity “File Aging Report” `
-status $file.DirectoryName -currentoperation $file.name
switch ($age) {
{$age -ge 730} {$2yrs=$2yrs+1;$Total2yr=$Total2Yr+$file.length;`
break}
{$age -ge 365} {$1yr=$1yr+1;$Total1yr=$Total1Yr+$file.length;`
break}
{$age -ge 90} {$3Mo=$3Mo+1;$Total90=$Total90+$file.length;`
break}
{$age -ge 30} {$1Mo=$1Mo+1;$Total30=$Total30+$file.length;`
break}
{$age -ge 7} {$1wk=$1wk+1;$Total7=$Total7+$file.length;break}
{$age -lt 7} {$current=$current+1;$TotalCurrent=`
$TotalCurrent+$file.Length;break}
}
$server = hostname
$file | select Name,FullName,CreationTime,LastWriteTime,LastAccessTime,@{n='Length (KB)';e={($_.length)/1024}} | export-csv C:\temp\filereport_$server.csv -NoTypeInformation -Append
}
$GrandTotal=$Total2yr+$Total1yr+$Total90+$Total30+$Total7+$TotalCurrent
#format file size totals to MB
$GrandTotal=”{0:N2}” -f ($GrandTotal/1048576)
$Total2yr=”{0:N2}” -f ($Total2yr/1048576)
$Total90=”{0:N2}” -f ($Total90/1048576)
$Total1yr=”{0:N2}” -f ($Total1yr/1048576)
$Total30=”{0:N2}” -f ($Total30/1048576)
$Total7=”{0:N2}” -f ($Total7/1048576)
$TotalCurrent=”{0:N2}” -f ($TotalCurrent/1048576)
#clear-host
Write-Host “File Aging for” $dir.ToUpper()
Write-Host “2 years:” $2yrs “files” $Total2yr “MB” -foregroundcolor “Red”
Write-Host “1 year:” $1yr “files” $Total1yr “MB” -foregroundcolor “Magenta”
Write-Host “3 months:” $3mo “files” $Total90 “MB” -foregroundcolor “Yellow”
Write-Host “1 month:” $1mo “files” $Total30 “MB” -foregroundcolor “Cyan”
Write-Host “1 week:” $1wk “files” $Total7 “MB” -foregroundcolor “Green”
Write-Host “Current:” $current “files” $TotalCurrent “MB”
Write-Host `n
Write-Host “Totals:” $count “files” $GrandTotal “MB”
Write-Host `n
$props = [ordered]@{
FilePath = $dir
TwoYearFileCount = $2yrs
'TwoYearFileSize (MB)' = $Total2yr
OneYearFileCount = $1yr
'OneYearFileSize (MB)' = $Total1yr
ThreeMonthFileCount = $3mo
'ThreeMonthFileSize (MB)' = $Total90
OneMonthFileCount = $1mo
'OneMonthFileSize (MB)' = $Total30
OneWeekFileCount = $1wk
'OneWeekFileSize (MB)' = $Total7
NewFileCount = $current
'NewFileSize (MB)' = $TotalCurrent
TotalCount = $count
'TotalSize (MB)' = $GrandTotal
}
$obj = new-Object -TypeName PSOBject -Property $props
$Obj | export-csv C:\temp\filesummary_$server.csv -NoTypeInformation -Append
}
else {
Write-Host “Failed to find” $Dir.ToUpper() -foregroundcolor “Red” `
-backgroundcolor “Black”
}
#usage PS C:\scripts> Dir F: | foreach {.\File-Aging.ps1 $_.FullName}
@pstakuu

pstakuu commented Aug 12, 2022

Copy link
Copy Markdown
Author

I can't remember where I got this from, but I updated it to be pure host-output to be an object for exporting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment