function Get-StaleDatastoreDirs {
<#
.SYNOPSIS
Get a list of datastore directories where all files are inactive for more than $StaleAfterDays days 
.DESCRIPTION
Get-StaleDatastoreDirs traverses top level datastore directories looking for any directory where all
the files within haven't been written to for over $StaleAfterDays days.  The default is >30 days
.EXAMPLE
Get-StaleDatastoreDirs

DESCRIPTION
-----------
This command will run against all datastores and use a default value of 30 for $StaleAfterDays

.EXAMPLE
Get-StaleDatastoreDirs -StaleAfterDays 60 -TargetDatacenter "My Datacenter" -SkipNonSharedDatastores

DESCRIPTION
-----------
This command will run against datastores in the specified datacenter and return any that have over 60
days of inactivity.  It will also skip any non-shared datastores
#>

    [CmdletBinding()] param (
        [Parameter(Mandatory=$false)] [Int]$StaleAfterDays = 30,
        [Parameter(Mandatory=$false)] [Switch]$SkipNonSharedDatastores,
        [Parameter(Mandatory=$false)] [String]$TargetDatacenter
    )

    process {

        $current = Get-Date
        $listOfStaleDirs = @()
        
        try {
            if( $SkipNonSharedDatastores ) {
                if( $TargetDatacenter ) {
                    $dsList = Get-Datastore -Location (Get-Datacenter $TargetDatacenter -ErrorAction Stop) -ErrorAction Stop | ?{$_.ExtensionData.Summary.MultipleHostAccess}
                }
                else {
                    $dsList = Get-Datastore -ErrorAction Stop | ?{$_.ExtensionData.Summary.MultipleHostAccess}
                }
            }
            else {
                if( $TargetDatacenter ) {
                    $dsList = Get-Datastore -Location (Get-Datacenter $TargetDatacenter -ErrorAction Stop) -ErrorAction Stop
                }
                else {
                    $dsList = Get-Datastore -ErrorAction Stop
                }
            }
            $dsList = $dsList | sort Name
        }
        catch {
            Write-Host $_ -ForegroundColor Magenta
            return
        }
        
        foreach ($datastore in $dsList) {

            Write-Host "Scanning datastore:" $datastore.Name
            New-PSDrive -Location $datastore -Name ds -PSProvider VimDatastore -Root '\' | Out-Null
            
            [array]$dirs = Get-ChildItem -Path 'ds:\' | ?{$_.Name -ne ".vSphere-HA" -and $_.ItemType -eq "Folder"} | sort Name
            
            foreach ($dir in $dirs) {
                Write-Host "`tScanning directory:" $dir.Name
                $numOfStaleFiles = 0
                [array]$subfiles = Get-ChildItem -Path $dir.FullName -Recurse | ?{$_.ItemType -ne "Folder"}
                
                #check to see if current file has been written to in $staleAfterDays num of days
                foreach ($file in $subfiles) {
                    if ( ($current.Subtract($file.LastWriteTime)).TotalDays -gt $StaleAfterDays ) {
                        $numOfStaleFiles++
                    }
                }
                
                #if all files in directory haven't been written to in $staleAfterDays num of days
                if($subfiles.length -eq $numOfStaleFiles) {
                    Write-Host "`tFound directory stale for over $StaleAfterDays days:" $dir.Name -ForegroundColor Cyan
                    $custDirObj = New-Object PSObject -Property @{
                        DatastoreName = $datastore.Name
                        DirectoryName = $dir.Name
                    }
                    $listOfStaleDirs += $custDirObj
                }
            }
            Remove-PSDrive ds
        }

        return $listOfStaleDirs
    }
}