Powershell 文件夹大小排序,自动大小转换
Powershell folder size sort with auto size conversion
我有一个 powershell 脚本,它获取文件夹名称、上次写入时间、大小
cd \myfolder
get-childitem | where {$_.PSIsContainer} | foreach {
$size = ( Get-ChildItem $_ -Recurse -Force | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum
$obj = new-object psobject
add-member -inp $obj noteproperty path $_.Name
add-member -inp $obj noteproperty time $_.LastWriteTime
add-member -inp $obj noteproperty size $size
$obj
}
导致Path/Name、LastWriteTime、大小
path time size
---- ---- ----
Audit_data_network_8-FEB-2017 2/8/2017 10:59:33 AM 1071084
dec-2015 1/25/2016 10:05:07 AM 29742775
games 2/15/2016 11:33:02 AM 52134862
kolachi 12/2/2015 3:37:27 PM 12487862
lighroom_ 5/29/2015 2:13:10 PM 2788765
Mini_Remote_Control7.5.9.0_Portable 6/13/2014 3:58:08 PM 52406834
ps 2/20/2017 4:23:10 PM 126707
totalcmd 1/25/2017 4:20:48 PM 11113908
我想修改结果以添加以下功能
- 在 KB/Mb/GB 中自动显示尺寸
- 按大小排序结果(上面最常用的文件夹大小)
要添加自动转换功能,我在某个论坛找到了
# If ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
#ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
#ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
#ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
#ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
#ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" }
#Write-Output $sizeOutput
但是由于我对这个脚本问题缺乏了解,我真的被过去 2-3 天困住了,如何将它与现有脚本合并并获得我想要的
结果 ...
任何帮助将不胜感激...
你可以把逻辑放在ex。一个可重复使用的函数,并使用它为 size
生成 属性 值。在此解决方案中,我使用了计算的 属性 而不是函数。这样我们就可以保留原始大小(长度之和)进行排序并在之后修改输出。我还简化了您的对象创建。例如:
Get-FoldersWithSize.ps1:
param ($Path = ".")
$PrettySizeColumn = @{name="Size";expression={
$size = $_.Size
if ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" }
$sizeOutput
}}
Get-ChildItem -Path $Path | Where-Object {$_.PSIsContainer} | ForEach-Object {
$size = ( Get-ChildItem -Path $_.FullName -Recurse -Force | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum
$obj = new-object -TypeName psobject -Property @{
Path = $_.Name
Time = $_.LastWriteTime
Size = $size
}
$obj
} | Sort-Object -Property Size -Descending | Select-Object Path, Time, $PrettySizeColumn
用法:
#Current directory (default value)
.\Get-FoldersWithSize.ps1
#Relative path
.\Get-FoldersWithSize.ps1 -Path ".\Downloads"
#Absolute path
.\Get-FoldersWithSize.ps1 -Path "C:\ProgramData"
我有一个 powershell 脚本,它获取文件夹名称、上次写入时间、大小
cd \myfolder
get-childitem | where {$_.PSIsContainer} | foreach {
$size = ( Get-ChildItem $_ -Recurse -Force | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum
$obj = new-object psobject
add-member -inp $obj noteproperty path $_.Name
add-member -inp $obj noteproperty time $_.LastWriteTime
add-member -inp $obj noteproperty size $size
$obj
}
导致Path/Name、LastWriteTime、大小
path time size
---- ---- ----
Audit_data_network_8-FEB-2017 2/8/2017 10:59:33 AM 1071084
dec-2015 1/25/2016 10:05:07 AM 29742775
games 2/15/2016 11:33:02 AM 52134862
kolachi 12/2/2015 3:37:27 PM 12487862
lighroom_ 5/29/2015 2:13:10 PM 2788765
Mini_Remote_Control7.5.9.0_Portable 6/13/2014 3:58:08 PM 52406834
ps 2/20/2017 4:23:10 PM 126707
totalcmd 1/25/2017 4:20:48 PM 11113908
我想修改结果以添加以下功能
- 在 KB/Mb/GB 中自动显示尺寸
- 按大小排序结果(上面最常用的文件夹大小)
要添加自动转换功能,我在某个论坛找到了
# If ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
#ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
#ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
#ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
#ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
#ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" }
#Write-Output $sizeOutput
但是由于我对这个脚本问题缺乏了解,我真的被过去 2-3 天困住了,如何将它与现有脚本合并并获得我想要的 结果 ... 任何帮助将不胜感激...
你可以把逻辑放在ex。一个可重复使用的函数,并使用它为 size
生成 属性 值。在此解决方案中,我使用了计算的 属性 而不是函数。这样我们就可以保留原始大小(长度之和)进行排序并在之后修改输出。我还简化了您的对象创建。例如:
Get-FoldersWithSize.ps1:
param ($Path = ".")
$PrettySizeColumn = @{name="Size";expression={
$size = $_.Size
if ( $size -lt 1KB ) { $sizeOutput = "$("{0:N2}" -f $size) B" }
ElseIf ( $size -lt 1MB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1KB)) KB" }
ElseIf ( $size -lt 1GB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1MB)) MB" }
ElseIf ( $size -lt 1TB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1GB)) GB" }
ElseIf ( $size -lt 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1TB)) TB" }
ElseIf ( $size -ge 1PB ) { $sizeOutput = "$("{0:N2}" -f ($size / 1PB)) PB" }
$sizeOutput
}}
Get-ChildItem -Path $Path | Where-Object {$_.PSIsContainer} | ForEach-Object {
$size = ( Get-ChildItem -Path $_.FullName -Recurse -Force | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum
$obj = new-object -TypeName psobject -Property @{
Path = $_.Name
Time = $_.LastWriteTime
Size = $size
}
$obj
} | Sort-Object -Property Size -Descending | Select-Object Path, Time, $PrettySizeColumn
用法:
#Current directory (default value)
.\Get-FoldersWithSize.ps1
#Relative path
.\Get-FoldersWithSize.ps1 -Path ".\Downloads"
#Absolute path
.\Get-FoldersWithSize.ps1 -Path "C:\ProgramData"