Miguel
Miguel

Reputation: 91

Get-ChildItem inside the A Get-ChildItem

I have a question about a script I am writing. I need a script that can find all folders inside a specific folder (in this case WRA), but I also want to find the sub-folders inside WRA For example: \Server01\WRA contains 100 folders \Server01\WRA\Folder 1 \Server01\WRA\Folder 2 ... ...

I want to look inside the "Folder 1" and obtain a report of the folders inside "Folder 1" and locate the folders older than 30 days which can be done with

Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))}

This is what I have so far

$FolderNames = Get-ChildItem "\\Server01\WRA\" -Directory | 
    Select-Object Name, LastWriteTime

$FolderNames

Thank you for all your help

Upvotes: 1

Views: 1206

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60045

You can use this:

Get-ChildItem "\\Server01\WRA\" -Directory |
Get-ChildItem -Directory |
Where-Object -Property LastWriteTime -LT ([datetime]::Now).AddDays(-30) |
Select-Object Name, LastWriteTime

Edit

$directories = Get-ChildItem "\\Server01\WRA\" -Directory
$targetDate = ([datetime]::Now).AddDays(-30)

foreach($parentDir in $directories)
{
    $subFolders = Get-ChildItem $parentDir -Directory |
    Where-Object -Property LastWriteTime -LT $targetDate
    
    if($subFolders)
    {
        "Parent Directory: {0}" -f $parentDir.FullName
    }

    foreach($childDir in $subFolders)
    {
        "Parent SubFolder: {0}" -f $childDir.FullName

        $childDir | Select-Object Name, LastWriteTime | Out-String
    }
}

Upvotes: 1

Related Questions