Powershell 2.0版本下的脚本:
1
2
3
4
5
6
7
8
9
10
11
12
|
<#
Script Function: get size of folder
Script Editor: Snail Yu
#>
$directories
=
Get-ChildItem
c:\windows
|where
{
$_
.mode
-like
"d*"
}
foreach
(
$directory
in
$directories
){
$files
=(
Get-ChildItem
$directory
.fullname -Recurse -ErrorAction SilentlyContinue
|where
{
$_
.mode
-like
"-a*"
})
foreach
(
$file
in
$files
){
$size
=
$size
+
$file
.length
}
write-host
"the size of $directory is : $size"
}
|
PowerShell 4.0版本下的脚本:
1
2
3
4
5
6
7
8
9
10
11
12
|
<#
Script Function: get size of folder
Script Editor: Snail Yu
#>
$directories
=
Get-ChildItem
d:\ -Directory
foreach
(
$directory
in
$directories
){
$files
=
Get-ChildItem
$directory
.FullName -Recurse –File -ErrorAction SilentlyContinue
foreach
(
$file
in
$files
){
$size
=
$size
+
$file
.Length
}
write-host
"the size of $directory is : $size"
}
|
参数说明:
(1)powershell 2.0中的get-childitem 没有file和directory的参数,只有通过管道来筛选;
(2)-erratction为错误处理机制,详见博客:http://281816327.blog.51cto.com/907015/1417587
(3)powershell得到的是文件夹名不包括绝对路径,需要用fullname属性显示指定;
本文转自 bannerpei 51CTO博客,原文链接:http://blog.51cto.com/281816327/1549025,如需转载请自行联系原作者