TheTaylorLee / AdminToolbox

Repository for the AdminToolbox PowerShell Modules
MIT License
191 stars 23 forks source link

AdminToolbox.Filemanagement - Get-StaleDirectories #129

Closed TheTaylorLee closed 1 year ago

TheTaylorLee commented 1 year ago

Feature Target Details

Describe the solution you'd like This is a function to find inactive folders. It does this be looking for last written to files for each subfolder of a top-level directory. It then returns datetimes for the last time any file in those subfolders was edited. This can be used for cleaning up certain file shares like user files shares.

Code Sample

$path="d:\user"
$NoOfDirs=Get-ChildItem $path | Where-Object {$_.PSIsContainer -eq $True} 
$results = ForEach($dir in $NoOfDirs ) {
    Get-ChildItem  "$path\$($dir.name)" -Recurse | 
    Where-Object { ($_.PSIsContainer -eq $False) } | 
    Select-Object @{l='Folder';e={$dir.Name}},Name,LastWriteTime | 
    Sort-Object  -pro LastWriteTime -Descending |
    Select-Object -First 1
}
$results | export-csv $env:USERPROFILE\downloads
TheTaylorLee commented 1 year ago

Creating function with name Get-LastUsedDirectory