### This one method was shamelessly ripped off from ### https://blogs.technet.microsoft.com/pstips/2017/05/20/display-friendly-file-sizes-in-powershell/ ### because I found it useful. Credit is presumed to goto Martin Schvartzman as listed at the time. function Get-FriendlySize { param($Bytes) $sizes='Bytes,KB,MB,GB,TB,PB,EB,ZB' -split ',' for($i=0; ($Bytes -ge 1kb) -and ($i -lt $sizes.Count); $i++) {$Bytes/=1kb} $N=2; if($i -eq 0) {$N=0} "{0:N$($N)} {1}" -f $Bytes, $sizes[$i] } ## Archive-Walk-Path is a shameful thrown together function/script ## written by me, Jonathan P function Archive-Walk-Path($Age, $base, $destination, $Path, $refDate=(Get-Date), $Depth=0, $inInfo) { $Length = 0 $info = @{ "FileCount"=0 "DirCount"=0 "Length"=0 "Failed"=0 "LastLength"=0 }; if ($inInfo -ne $null) { $info = $inInfo } if ($Path -eq $null) { $newBase = (Get-Item $base) $newDest = (Get-Item $destination) Write-Host 'Source ' $base ' -> ' $newBase.FullName Write-Host 'Destination ' $destination ' -> ' $newDest.FullName $info = (Archive-Walk-Path $Age $newBase.FullName $newDest.FullName $newBase.FullName $refDate $Depth $info) } else { $directories = Get-ChildItem $Path $info["DirCount"] = ($info["DirCount"] + 1) $info["LastLength"] = 0 if ($directories -eq $null) { return $info; } $i = 0 $sumLength = 0 foreach($dir in $directories) { if ($dir -eq $null) { continue } if (!(Test-Path -Path $dir -PathType Leaf)) { $newLength = (Archive-Walk-Path $Age $base $destination $dir.FullName $refDate ($Depth + 1) $info).LastLength $sumLength += $newLength if ($Depth -eq 0) { $i++ if ($i -gt 10) { $i=0 Write-Host $dir.FullName 'Size: ' (Get-FriendlySize $sumLength) ' -- Total: ' (Get-FriendlySize ($info["Length"] + $newLength)) ' -- File Count: ' $info["FileCount"] ' -- Dir Count: ' $info["DirCount"] ' -- Fail: ' $info["Failed"] $sumLength = 0 } } } elseif ($refDate - $dir.LastWriteTime -gt $Age) { $newPath = (Join-Path $destination $dir.FullName.Replace($base,'')) #Write-Host ' ('$dir.Length ') - ' $dir.FullName ' -> ' $newPath $info["Length"] = $info["Length"] + $dir.Length $info["LastLength"] = $info["LastLength"] + $dir.Length try { #Do Something $destPath = (Split-Path $newPath) if (!(Test-Path -LiteralPath $destPath)) { New-Item -ItemType Directory -Force -Path $destPath } Move-Item -Force -LiteralPath $dir.FullName -Destination $newPath $info["FileCount"] = $info["FileCount"] + 1 } catch { Write-Warning 'Exception during move: ' $_ $info["Failed"] = $info["Failed"] + 1 } } } } return $info; } ## Usage $endInfo = (Archive-Walk-Path (new-timespan -days 365) "." "C:\archive") Write-Host 'Total: ' $Length ' -- ' (Get-FriendlySize $endInfo["Length"]) ' -- File Count: ' -- File Count: ' $endInfo["FileCount"] ' -- Dir Count: ' $endInfo["DirCount"] ' -- Fail: ' $endInfo["Failed"]