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:
- Iterate through each top-level directory in the source path.
- For each top-level directory, find the oldest file within that directory and its subdirectories.
- Create a custom object for each top-level directory containing the directory's properties and the oldest file's properties.
- Collect these custom objects in an array.
- 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.