Powershell 之批量获取文件大小的实现代码
效果图:

核心代码
$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
相关推荐
higheels 2020-08-03
ZoctopusD 2020-08-03
酷云的csdn 2020-08-03
higheels 2020-07-27
liushun 2020-06-28
zhendeshifeng 2020-06-22
Sabrina 2020-06-11
CARBON 2020-06-03
CARBON 2020-06-01
DBATips 2020-05-31
higheels 2020-05-29
applecarelte 2020-04-15
Yyqingmofeige 2020-03-28
yoshubom 2020-03-06
Yyqingmofeige 2020-03-02
SciRui 2020-02-26