Get the details of all first level folders and the oldest file in them recursively

alhowarthWF 296 Reputation points
2024-03-27T18:30:30.3733333+00:00

I have two scripts that do what I need, but really need them to be combined to save manual matching.

Script 1 gathers all the top-level folders and Create & Last Write dates and Owner:

$sourcePath = 'P:\'              # enter your path here
$outputPath = 'C:\Temp\x-list4.txt'   # and the path for the output file here
Get-ChildItem -Path $sourcePath  | 
Select-Object Mode, FullName, CreationTime, LastWriteTime, LastAccessTime,
              @{Name = 'Owner'; Expression = {(Get-Acl -Path $_.FullName).Owner}} |
Export-Csv -Path $outputPath -UseCulture -NoTypeInformation

Script 2 - I have to specify a top-level folder and run it to get the oldest file in that folder or sub-folders. And repeat for each top-level folder.

$FileDate = (Get-Date -Format g)
$path = “P:\Folder4\”
Get-ChildItem -Path $path -Recurse | ForEach-Object {
if ($_.LastWriteTime -lt $FileDate -and -not $_.PSIsContainer) {
$FileDate = $_.LastWriteTime
$OldFile = $_.FullName
}
}
Write-Host ‘oldest file on the system is: ‘ $OldFile
$FileDate

Is there a way to combine them so each row would have the top level folder's info and the oldest file in the folder and subfolders?

FullName CreationTime LastWriteTime Owner OldestFile OldestFileWriteTime

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,059 questions
0 comments No comments
{count} votes

Accepted answer
  1. guilherme rodrigues 160 Reputation points
    2024-03-27T23:31:30.65+00:00

    Hi @alhowarthWF ,

    Try something like this:

    $sourcePath = 'P:\'  # Enter your source path here
    
    $outputPath = 'C:\Temp\x-list4.csv'  # Enter your output file path here
    
    # Create an empty array to hold the results
    
    $results = @()
    
    # Get all top-level directories in the source path
    
    $topLevelFolders = Get-ChildItem -Path $sourcePath -Directory
    
    foreach ($folder in $topLevelFolders) {
    
        $oldestFile = Get-ChildItem -Path $folder.FullName -Recurse | Where-Object { -not $_.PSIsContainer } | Sort-Object LastWriteTime | Select-Object -First 1
    
        $result = [PSCustomObject]@{
    
            FullName            = $folder.FullName
    
            CreationTime        = $folder.CreationTime
    
            LastWriteTime       = $folder.LastWriteTime
    
            Owner               = (Get-Acl -Path $folder.FullName).Owner
    
            OldestFile          = $oldestFile.FullName
    
            OldestFileWriteTime = $oldestFile.LastWriteTime
    
        }
    
        # Add the result to the results array
    
        $results += $result
    
    }
    
    # Export the results to a CSV file
    
    $results | Export-Csv -Path $outputPath -UseCulture -NoTypeInformation
    
    

    This script will:

    1. Iterate through each top-level directory in the source path.
    2. For each top-level directory, find the oldest file within that directory and its subdirectories.
    3. Create a custom object for each top-level directory containing the directory's properties and the oldest file's properties.
    4. Collect these custom objects in an array.
    5. Export the array to a CSV file at the specified output path.

    This should give you a CSV file with a row for each top-level folder and the requested information for that folder and its oldest file.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,731 Reputation points Microsoft Vendor
    2024-03-27T23:47:13.1033333+00:00

    Hi alhowarthWF,

    Let's see if this meets your needs.

    $sourcePath = 'P:\'              # enter your path here
    $outputPath = 'C:\Temp\x-list4.txt'   # and the path for the output file here
    Get-ChildItem -Path $sourcePath | 
    Select-Object Mode, FullName, CreationTime, LastWriteTime, LastAccessTime,
                  @{Name = 'Owner'; Expression = {(Get-Acl -Path $_.FullName).Owner}} |
    ForEach-Object {
        $FileDate = (Get-Date -Format g)
        #$path = “C:\Temp\a”
        Get-ChildItem -Path $_.FullName -Recurse | ForEach-Object {
            if ($_.LastWriteTime -lt $FileDate -and -not $_.PSIsContainer) {
                $FileDate = $_.LastWriteTime
                $OldFile = $_.FullName
            }
        }
        
        Add-member -InputObject $_ -Name "OldestFile" -Value $OldFile -MemberType NoteProperty
        Add-member -InputObject $_ -Name "OldestFileWriteTime" -Value $FileDate -MemberType NoteProperty
        $_
    }|          
    Export-Csv -Path $outputPath -UseCulture -NoTypeInformation
    

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.