Powershell 如何批量獲取文件大小的實現代碼

這篇文章主要介紹了Powershell 之批量獲取文件大小的實現代碼
效果圖:

Powershell 如何批量獲取文件大小的實現代碼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

原文來自:https://www.jb51.net/article/98381.htm

本文地址:https://www.linuxprobe.com/powershell-set-executionpolicy.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章