# Identify unused folders

**URL:** <https://forums.powershell.org/t/identify-unused-folders/20810>\
**Category:** PowerShell Help\
**Created:** [November 2, 2022, 9:17am UTC](https://forums.powershell.org/t/identify-unused-folders/20810 "2022-11-02T09:17:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomroos](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@thomroos](https://forums.powershell.org/u/thomroos)\
**Post date:** [November 2, 2022, 9:17am UTC](https://forums.powershell.org/t/identify-unused-folders/20810/1 "2022-11-02T09:17:44Z")

</div>

I’m trying to identify folders on a data share which haven’t been used in a long time.  
My idea was to loop through all folders, and for each folder loop through all files to get the LastAccessedDate. For testing purposes I’m now trying to list all folders and identify the file with the latest LastAccessedDate. However, I only get one result, which isn’t even in the folder I specified.  
Probably something simple I’m missing, but would be glad if someone could help me out.  
Or, if there’s an easier way to accomplish the same thing, I’d like to hear it.  
Here’s my code:

```auto
Function Get-Folders
{
    Param([string[]]$path)

    # Get all subfolders
    $folders = Get-ChildItem -Path $path -Recurse -Directory -Force

    # Loop through all files in each subfolder and display newest lastaccesseddate
    foreach ($folder in $folders)
    {
        $lastDate = Get-Date "01/01/1970"
        $lastFile = $null

        $files = Get-ChildItem -Path $folder -File -Force -ErrorAction SilentlyContinue

        foreach ($file in $files)
        {
            if ($file.LastAccessTime -gt $lastDate) 
            { 
                $lastFile = $file
                $lastDate = $file.LastAccessTime
            }
        }

        Write-Output $lastFile.FullName
        Write-Output $lastFile.LastAccessTime
    }
}

```

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [November 2, 2022, 12:40pm UTC](https://forums.powershell.org/t/identify-unused-folders/20810/2 "2022-11-02T12:40:25Z")

</div>

No need to create a custom function to search for files by lastaccesstime. Use below.

```auto
Get-ChildItem $path -Recurse -File | Where-Object {$_.LastAccessTime -gt $lastDate}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 2, 2022, 1:05pm UTC](https://forums.powershell.org/t/identify-unused-folders/20810/3 "2022-11-02T13:05:18Z")

</div>

> [@thomroos](#):
>
> loop through all files

👆🏽 !! For some file types a `Get-ChildItem` updates the `LastAccessTime` !! 👆🏽

… you may check that in advance with test files

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/identify-unused-folders/20810/4 "2024-05-16T20:16:28Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


