計量文件夾大小

Posted on 2019-12-21 20:34  努力成长静待花开  阅读(208)  评论(0编辑  收藏  举报
##Using .com objects
function DirWithSize ($path=$PWD)
{
    $fs=New-Object -com scripting.filesystemobject
    Get-ChildItem $path|Format-Table -AutoSize Mode,LastWriteTime,Name,
    @{ Label="Lenght(KB)";alignment="Left";Expression={
        if($_.PSIsContainer){$fs.GetFolder($_.Fullname).Size/1KB}
        else
            {$_.length/1KB}
        }
    }
}
##Using measure count
function DirWithSize ($path=$pwd)
{
    ls $path |ft -au mode,lastwritetime,name,
    @{ Label="lenght(kb)";alignment="left";Expression={
        if($_.PsIsContainer){(ls $_.fullname -r -file|measure length -Sum).sum/1Kb}
        else
          {$_.length/1KB}
        }
    }
}

簡介:

  為什麼會有連個不同的例子呢? 其實如果你在C盤的更目錄執行這兩個函數你就會發現