将以下脚本命名为test.ps1, 并在powershell下运行.\test.ps1即可。
#list all folder and file size of specific filepath folder
function filesize ([string]$filepath)
{
if ($filepath -eq $null)
{
throw "file path cannot be blank"
}
$_.name + "file size(MB)" -f $l | Out-File ($filepath+"test.txt")
dir -Path $filepath |
ForEach-Object -Process {
if ($_.psiscontainer -eq $true)
{#folder size
$length = 0
dir -Path $_.fullname -Recurse | ForEach-Object{
$length += $_.Length
}
$l = $length/1MB
# output to the console
$_.name + "folder size is: {0:n2} MB" -f $l
# save to test.txt file
$_.name + " {0:n2}" -f $l | Out-File -Append ($filepath+"_test.txt")
}else
{#file size
$length = 0
dir -Path $_.fullname -Recurse | ForEach-Object{
$length += $_.Length
}
$l = $length
$_.name + " file size is: {0:n2} B" -f $l
$_.name + " {0:n2}" -f $l | Out-File -Append ($filepath+"_test.txt")
}
}
}
filesize -filepath "C:\Program File"
如果是第一次运行需要开启执行脚本权限。
在powershell中运行如下命令,然后 Y 确认。
开启:set-executionpolicy remotesigned
关闭:Set-ExecutionPolicy Restricted