Powershell 获取ChildItem高内存使用率

Powershell 获取ChildItem高内存使用率,powershell,memory,get-childitem,Powershell,Memory,Get Childitem,我想扫描一些包含数百万个文件的大目录,以获取二级目录名、大小和每个目录中的文件数的摘要。我留下的代码运行了几个小时,但它被困在中间的某个地方,因为PosithSu壳消耗13GB的内存。你知道为什么内存使用率这么高吗 $mailboxes = Get-ChildItem E:\data -Directory | Get-ChildItem -Directory | Select -ExpandProperty FullName foreach

我想扫描一些包含数百万个文件的大目录,以获取二级目录名、大小和每个目录中的文件数的摘要。我留下的代码运行了几个小时,但它被困在中间的某个地方,因为PosithSu壳消耗13GB的内存。你知道为什么内存使用率这么高吗

$mailboxes = Get-ChildItem E:\data -Directory |
             Get-ChildItem -Directory |
             Select -ExpandProperty FullName

foreach ($line in $mailboxes) {
    $s = $line.split("\\")
    $files = Get-ChildItem $line -File -Recurse -Include *.eml
    $mailbox_path = $line
    $mailbox = $s[-1]
    $size = $files | Measure-Object Length -Sum | select -ExpandProperty Sum
    $count = $files.Count
    "$mailbox_path`t$mailbox`t$size`t$count" |
        Out-File 'D:\scripts\summary.txt' -Append
}

看看这是否更好

$mailboxes = Get-ChildItem E:\data -Directory | Get-ChildItem -Directory
$OutputPath = 'D:\scripts\summary.csv'

foreach($line in $mailboxes) {

$files = Get-ChildItem $line.FullName -File -Recurse -Filter *.eml
$size = $files | Measure-Object Length -Sum | select -ExpandProperty Sum


[pscustomobject]@{MailBoxPath = $line.FullName;MailBox = $line.BaseName;FileSize = $size;FileCount = $files.count} | Export-Csv -Path $OutputPath -NoTypeInformation -Append
}

内存的使用是否随着脚本的运行而逐渐增加,或者只是在它上升的中间?当替换<代码> -Copy**.EML<代码> > <代码> >筛选器*.EML<代码>和/或仅收集变量<代码>文件$> /代码>中的文件大小时,它会有帮助吗?(
$files=Get ChildItem…|选择对象-展开长度
)?正确使用PowerShell管道还应尽量减少内存消耗。要正确使用PowerShell管道,应避免变量分配(如
$mailboxes=
)和括号,特别是对于平均流。因此:
getchilditem E:\data-Directory | getchilditem-Directory | ForEach对象{getchilditem$line.FullName-File-Recurse-Filter*.eml…}导出Csv-Path$OutputPath-NoTypeInformation-Append