以下是powershell 下面的代码(代码在图最下方)。
1.记得要用管理员模式启动powershell
2.代码记得保存为psl 为后缀的文件
3.运行时候要免签名按照我下面的命令格式运行就可以。
powershell -ExecutionPolicy Bypass -File .\findsize.ps1 -RootPath "C:\Users\Administrator"-SizeThresholdMB 500
#给小白门解释一下
1.findsize.ps1是代码保存的文件,你可以任意起名。.\就不用解释了吧,路径而已,你在不同的路径需要更改。
2.-RootPath "C:\Users\Administrator" 这里引号里面是你要查找文件夹大小的路径。
3.-SizeThresholdMB 500 ,这里是查找大于500的文件夹,并且举例出来。500这个参数是你按照你需要随便填写。
以下是代码段:
# 2024_5_16_By_ZS执行命令的方法 powershell -ExecutionPolicy Bypass -File .\findsize.ps1 -RootPath "C:\Users\Administrator"-SizeThresholdMB 500
param (
[Parameter(Mandatory=$true)]
[string]$RootPath,
[Parameter(Mandatory=$false)]
[int]$SizeThresholdMB = 1000
)
function GetFolderSize {
param (
[string]$folderPath
)
try {
$items = Get-ChildItem -LiteralPath $folderPath -Recurse -File -Force -ErrorAction Stop | Measure-Object -Property Length -Sum
return [math]::Round($items.Sum / 1MB, 2)
} catch {
return -1
}
}
function FindLargeFolders {
param (
[string]$rootPath,
[int]$sizeThresholdMB
)
Get-ChildItem -LiteralPath $rootPath -Directory -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
try {
$folderPath = $_.FullName
$folderSize = GetFolderSize -folderPath $folderPath
if ($folderSize -gt $sizeThresholdMB) {
Write-Output "$folderPath - $folderSize MB"
}
} catch {
# 忽略错误
}
}
}
# 调用函数开始执行
try {
FindLargeFolders -rootPath $RootPath -sizeThresholdMB $SizeThresholdMB
} catch {
Write-Error "脚本执行过程中发生错误: $_"
}
祝大家好运!