检查是否存在特定大小的文件夹

Check if folder with a specific size exists

从执行的 Get-ChildItem 命令中,我想检查是否存在大小大于 1gb 的文件夹。由于特定原因,gci命令不能直接用Where-Object {$psiscontainer}执行,必须在gci执行后进行过滤。

编辑 稍微重新排列了代码。

这应该有所帮助,我使用了你使用 Scripting.FileSystemObject 的想法,并将函数修改为 return 所有大于提供的 $minsize

的文件夹
function Get-FolderBySize {  
   param (
        [string]$path,
        [Int32]$minSize # in GB
    )

    $folders = gci -Path $Path -Recurse -Force -ErrorAction SilentlyContinue | ? {$_.PSisContainer -eq $true}
    $FileSystemObject = New-Object -ComObject  Scripting.FileSystemObject   

    foreach ($folder in $folders)
    { 
        $size = $FileSystemObject.GetFolder($folder.FullName).Size / 1GB

        if ($size -ge $minSize)
        {
            $size = "{0:N}" -f $size
            Write-host "Folder $($folder.Fullname) has a size of $size GB" 
            $folder
        }
    }
}