获取子目录的大小并写入文件Powershell窗口

时间:2019-09-19 08:50:41

标签: powershell

我有一个挂载的目录Z:\,并且它的目录下有一个名为Main的目录,下面有很多子目录,我想知道每个直接子目录(而不是子目录)的大小并将其写入在文件中。因此,例如,如果Main具有3个子目录ABC,那么我希望文本文件中的内容看起来像这样

Z:\Main\A 153MB
Z:\Main\B 2.3GB
Z:\Main\C 1.2TB

,依此类推。有什么建议可以在Powershell中完成吗?

3 个答案:

答案 0 :(得分:0)

尝试一下-

$array= @() 
$folder = "Z:\Main" 
Get-ChildItem $folder -Recurse -Depth 0 | Where-Object { $_.PSIsContainer } | 
ForEach-Object { 
    $obj = New-Object PSObject

    $Size = [Math]::Round((Get-ChildItem -Recurse $_.FullName | Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1KB, 2) 
    $obj |Add-Member -MemberType NoteProperty -Name "Path" $_.Name 
    $obj |Add-Member -MemberType NoteProperty -Name "SizeMB" $Size 
    $array +=$obj 
}

$array | select Path,SizeMB |  Export-Csv $env:USERPROFILE\Desktop\OutputFile.csv -NoTypeInformation

答案 1 :(得分:0)

我正建议:

$path = 'C:\Program Files'
$Folders = Get-ChildItem -Path $path -Directory -Recurse

foreach ($Folder in $Folders) {

    [PSCustomObject]@{
        Location = $Folder.fullname
        Size = [math]::Round(((Get-ChildItem -Path $Folder.fullname -File | Measure-Object -Property Length -Sum).Sum / 1MB),2)

    }
}

将数组中的项目添加到数组中并不是一个好主意,因为数组是静态的。 每次添加项目时,都会将数组复制到内存中,销毁并与新项目一起重新创建……这是非常昂贵的过程,应尽可能避免!

答案 2 :(得分:0)

这是前一段时间为一个朋友完成的……默认情况下是获取完整的子目录树大小。但是,-NoRecurse开关参数将使其仅给出每个子目录的大小,而没有任何更深的含义。

function Get-SubDirSize
    {
    [CmdletBinding ()]
    Param (
        [Parameter (
            Position = 0
            )]
            [string]
            $Path = $env:TEMP,

        [Parameter ()]
            [switch]
            $NoRecurse
        )

    begin {}

    process
        {
        $DirList = Get-ChildItem -LiteralPath $Path -Directory

        foreach ($DL_Item in $DirList)
            {
            $GCI_Params = @{
                LiteralPath = $DL_Item.FullName
                Recurse = -not $NoRecurse
                File = $True
                Force = $True
                ErrorAction = 'SilentlyContinue'
                }
            $FileList = Get-ChildItem @GCI_Params
            if ($FileList.Count -gt 0)
                {
                $DirSize = [math]::Round(($FileList |
                    Measure-Object -Property Length -Sum).Sum / 1MB, 2)
                }
                else
                {
                $DirSize = 0
                }

            [PSCustomObject]@{
                DirName = $DL_Item.FullName
                FileCount = $FileList.Count
                Size_MB = $DirSize
                }
            }
        }

    end {}

    } # end >>> function Get-SubDirSize

这样称呼...

Get-SubDirSize -Path $env:ProgramData -NoRecurse

...它给出以下内容...

DirName                                 FileCount Size_MB
-------                                 --------- -------
C:\ProgramData\Apple                            0       0
C:\ProgramData\Apple Computer                   0       0
C:\ProgramData\AVAST Software                   0       0
C:\ProgramData\Binary Fortress Software         0       0
C:\ProgramData\chocolatey                       5    6.48
C:\ProgramData\Git                              1       0
C:\ProgramData\IsolatedStorage                  0       0
C:\ProgramData\Last.fm                          0       0
C:\ProgramData\LogiShrd                         1       0
C:\ProgramData\Malwarebytes                     0       0
C:\ProgramData\Microsoft                        0       0
C:\ProgramData\MindGems                         0       0
C:\ProgramData\Mozilla                          0       0
C:\ProgramData\NCH Software                     0       0
C:\ProgramData\Notepad++                        0       0
C:\ProgramData\Oracle                           0       0
C:\ProgramData\Package Cache                    0       0
C:\ProgramData\Samsung                          0       0
C:\ProgramData\shimgen                          0       0
C:\ProgramData\Synology                         0       0