windows 用于获取目录总大小的 PowerShell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/809014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:23:05  来源:igfitidea点击:

PowerShell Script to Get a Directory Total Size

windowspowershellscriptingfilesystemssysadmin

提问by Jedi Master Spooky

I need to get the size of a directory, recursively. I have to do this every month so I want to make a PowerShellscript to do it.

我需要递归地获取目录的大小。我必须每个月都这样做,所以我想制作一个PowerShell脚本来做到这一点。

How can I do it?

我该怎么做?

回答by JaredPar

Try the following

尝试以下

function Get-DirectorySize() {
  param ([string]$root = $(resolve-path .))
  gci -re $root |
    ?{ -not $_.PSIsContainer } | 
    measure-object -sum -property Length
}

This actually produces a bit of a summary object which will include the Count of items. You can just grab the Sum property though and that will be the sum of the lengths

这实际上会产生一些汇总对象,其中将包括项目计数。你可以只获取 Sum 属性,这将是长度的总和

$sum = (Get-DirectorySize "Some\File\Path").Sum

EDITWhy does this work?

编辑为什么这有效?

Let's break it down by components of the pipeline. The gci -re $rootcommand will get all items from the starting $rootdirectory recursively and then push them into the pipeline. So every single file and directory under the $rootwill pass through the second expression ?{ -not $_.PSIsContainer }. Each file / directory when passed to this expression can be accessed through the variable $_. The preceding ? indicates this is a filter expression meaning keep only values in the pipeline which meet this condition. The PSIsContainer method will return true for directories. So in effect the filter expression is only keeping files values. The final cmdlet measure-object will sum the value of the property Length on all values remaining in the pipeline. So it's essentially calling Fileinfo.Length for all files under the current directory (recursively) and summing the values.

让我们按管道的组件分解它。该gci -re $root命令将$root递归地从起始目录中获取所有项目,然后将它们推送到管道中。因此, 下的每个文件和目录$root都将通过第二个表达式?{ -not $_.PSIsContainer }。传递给这个表达式的每个文件/目录都可以通过变量访问$_. 前面的 ? 表示这是一个过滤器表达式,意思是只保留管道中满足此条件的值。对于目录,PSIsContainer 方法将返回 true。所以实际上过滤器表达式只保留文件值。最终的 cmdlet measure-object 将对管道中剩余的所有值的属性 Length 的值求和。所以它本质上是为当前目录下的所有文件(递归)调用 Fileinfo.Length 并对值求和。

回答by Keith Hill

If you are interested in including the size of hidden and system files then you should use the -force parameter with Get-ChildItem.

如果您对包含隐藏文件和系统文件的大小感兴趣,那么您应该将 -force 参数与 Get-ChildItem 一起使用。

回答by Gordon Bell

Here's quick way to get size of specific file extensions:

这是获取特定文件扩展名大小的快速方法:

(gci d:\folder1 -r -force -include *.txt,*.csv | measure -sum -property Length).Sum

回答by youfoobar

Thanks to those who posted here. I adopted the knowledge to create this:

感谢那些在这里发帖的人。我采用了知识来创建这个:

# Loops through each directory recursively in the current directory and lists its size.
# Children nodes of parents are tabbed

function getSizeOfFolders($Parent, $TabIndex) {
    $Folders = (Get-ChildItem $Parent);     # Get the nodes in the current directory
    ForEach($Folder in $Folders)            # For each of the nodes found above
    {
        # If the node is a directory
        if ($folder.getType().name -eq "DirectoryInfo")
        {
            # Gets the size of the folder
            $FolderSize = Get-ChildItem "$Parent$Folder" -Recurse | Measure-Object -property length -sum -ErrorAction SilentlyContinue;
            # The amount of tabbing at the start of a string
            $Tab = "    " * $TabIndex;
            # String to write to stdout
            $Tab + " " + $Folder.Name + "   " + ("{0:N2}" -f ($FolderSize.Sum / 1mb));
            # Check that this node doesn't have children (Call this function recursively)
            getSizeOfFolders $Folder.FullName ($TabIndex + 1);
        }
    }
}

# First call of the function (starts in the current directory)
getSizeOfFolders "." 0