下面的所有的 Script
都是写在一个叫 filesize.ps1
文件下。
编辑 $PROFILE
配置(Microsoft.PowerShell_profile.ps1
),如下:
Import-Module "<路径>/fiesize.ps1"
PowerShell Script (Version 1):
Function filesize {
[cmdletbinding()]
Param($path='./' )
dir -Path $path -Force |
ForEach-Object -Process {
if ($_.PSIsContainer -eq $true)
{
$length = 0
dir -Path $_.fullname -Recurse -Force | ForEach-Object{
$length += $_.Length
}
$common = "`e[38;5;3m$($_.name)`e[0m: "
if ($length -gt 1GB) {
$l = $length/1GB
$common + "{0:n1} GB" -f $l
# $_.name + "文件夹的大小为: {0:n1} GB" -f $l
} elseif ($length -gt 1MB) {
$l = $length/1MB
$common + "{0:n1} MB" -f $l
# $_.name + "文件夹的大小为: {0:n1} MB" -f $l
} else {
$l = $length/1KB
$common + "{0:n1} KB" -f $l
}
} else {
$length = $_.Length
$common = "`e[38;5;33m$($_.name)`e[0m: "
if ($length -gt 1GB) {
$l = $length/1GB
$common + "{0:n1} GB" -f $l
} elseif ($length -gt 1MB) {
$l = $length/1MB
$common + "{0:n1} MB" -f $l
} else {
$l = $length/1KB
$common + "{0:n1} KB" -f $l
}
}
}
}
Example:
# 默认当前路径
PS> filesize
PS> filesize -path ../test
# 或者
PS> filesize -path "../test"