powershell 文件大小和信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 文件大小和信息相关的知识,希望对你有一定的参考价值。

#how to get files and ignore directories
$sizes = Get-ChildItem -Path C:\dell\suu -Recurse -ErrorAction Stop | Where-Object { ! $_.PSIsContainer } | Select-Object -ExpandProperty Length

#simple example
#---------------------------------------------------------
$path = "C:\Test"
#---------------------------------------------------------
$size = (Get-ChildItem -Path $path -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
"Folder Size: $size Bytes"
'Folder Size: {0:n2} MB' -f ($size/1MB)


#more in depth example
#---------------------------------------------------------
$path = "C:\test"
[double]$intSize = 0
[double]$totalNumFiles = 0
#---------------------------------------------------------
try{
    if(Test-Path $path){
        $files = Get-ChildItem -Path $path -Recurse -Force `
            -ErrorAction SilentlyContinue
    }
    else{
        Write-Output "The path you specified is not valid"
        return
    }
    #get information about files (combined size, total #)
    $i = 0
    foreach ($objFile in $files){
        $i++
        $intSize = $intSize + $objFile.Length
        Write-Progress -activity "Adding File Sizes" -status "Percent added: " `
            -PercentComplete (($i / $files.length)  * 100)
        $totalNumFiles++
    }
    #round for nice numbers
    $intSize = [math]::round($intSize / 1GB, 0)
    #generate output
    Write-Output "----------------------------------------------"
    Write-Output "Scan results for: $path"
    Write-Output "----------------------------------------------"
    Write-Output "Total # of file found: $totalNumFiles"
    Write-Output "----------------------------------------------"
    Write-Output "Total size of all files: $intSize GB."
    Write-Output "----------------------------------------------"
    Write-Output "Top 10 Largest Files found:"
    $files | select Directory,Name,`
    @{Label=”Length”;Expression={[math]::round($_.Length/1GB, 2)}} | `
        sort Length -Descending| select -First 10 | ft -AutoSize
    Write-Output "----------------------------------------------"
}
catch{
    Write-Error $_
}

#get file encoding information
$global:gitbin = 'C:\Program Files\Git\usr\bin'
Set-Alias file.exe $gitbin\file.exe
file.exe *

以上是关于powershell 文件大小和信息的主要内容,如果未能解决你的问题,请参考以下文章

用命令行设置powershell 屏幕缓冲区大小

如何递归获取PowerShell中的所有文件和文件夹的大小,自定义输出

PowerShell查找文件夹/文件大小

powershell PowerShell:获取与HP Print Driver DIFxAPI.dll问题有关的文件大小。

目录列表中的 powershell 文件大小总计

powershell 根据文件中的字节数转换文件大小单位