| 导读 | 这篇文章主要介绍了Powershell 之批量获取文件大小的实现代码Linux就该这么学 |
效果图:

核心代码:
$startFolder = "D:\"
$colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
{
$subFolderItems = (Get-ChildItem $i.FullName -recurse | Measure-Object -property length -sum)
$FileSize="{0:N2}" -f ($subFolderItems.sum / 1GB)
$Unit='GB'
if($FileSize -lt 1)
{
$FileSize="{0:N2}" -f ($subFolderItems.sum / 1MB)
$Unit='MB'
}
write-host $i.FullName ' -- ' $FileSize $Unit -fore green
}
注意:如果是第一次运行需要开启执行脚本权限。
在powershell中运行如下命令,然后 Y 确认即可。
开启:
set-executionpolicy remotesigned
关闭:
Set-ExecutionPolicy Restricted
该文章介绍了一段Powershell代码,用于批量计算指定目录及其子目录下所有文件的总大小,并以GB或MB为单位显示。通过设置执行权限后,在Powershell中运行此脚本,可以方便地获取文件系统的存储使用情况。
744

被折叠的 条评论
为什么被折叠?



