[玩转系统] 再次使用 PowerShell 测量文件夹
作者:精品下载站 日期:2024-12-14 07:46:29 浏览:15 分类:玩电脑
再次使用 PowerShell 测量文件夹
我知道我刚刚发布了测量文件夹功能的更新,但我无法控制自己,现在我有一个更新。部分更新是由于一条评论询问将结果格式化为一定数量的小数位而产生的。我通常使用 Math .NET 类中的 Round() 方法。
PS C:\> [math]::Round(1234.56789,2)
1234.57
PS C:\> [math]::Round(1234.5678900123,4)
1234.5679
所以我添加了一个参数Round,自动四舍五入到一定数量的小数点。默认值为 2,但您可以输入 0 到 10 之间的任何值。如果使用 0,则效果是将值视为整数。
我所做的另一个更改是简化代码。我创建 PowerShell 工具时的目的是不要有重复的命令或非常非常相似的命令。在上周的版本中,我使用 Switch 语句动态创建属性和值。但除了计量单位之外,每个项目实际上都是相同的。所以我想出了一个单位哈希表。
$unitHash = @{
Bytes = 1
KB = 1KB
MB = 1MB
GB = 1GB
TB = 1TB
PB = 1PB
}
这样,我可以使用哈希表键作为属性名称的一部分,以及用于格式化结果的值。
$value = [Math]::Round($stats.sum/$UnitHash.item($unit),$Round)
属性名称是为除默认“字节”之外的任何内容动态创建的。
$Label = "Size"
if ($unit -ne 'bytes') {
$label+= $($unit.ToUpper())
}
$propHash.Add($label,$value)
如果用户想要平均值,我会使用相同的过程。这是完整的修改后的功能。
#requires -version 4.0
Function Measure-Folder {
<#
.SYNOPSIS
Measure the size of a folder.
.DESCRIPTION
This command will take a file path and create a custom measurement object that shows the number of files and the total size. The default size will be in bytes but you can specify a different unit of measurement. The command will format the result accordingly and dynamically change the property name as well.
.PARAMETER Path
The default is the current path. The command will fail if it is not a FileSystem path.
.PARAMETER Average
Get the average file size. This will be formatted with the same unit as the total size.
.PARAMETER NoRecurse
The default behavior is to recurse through all subfolders. But you can suppress that by using -NoRecurse.
.PARAMETER Unit
The default unit of measurement is bytes, but you can use any of the standard PowerShell numeric shortcuts: "KB","MB","GB","TB","PB"
.PARAMETER Round
The number of decimal points to round the sum and average values. The default is 2. Use a value of 10 to not round. The maximum value is 10.
.EXAMPLE
PS C:\> measure-folder c:\scripts
Path Name Count Size
---- ---- ----- ----
C:\scripts scripts 2858 43800390
Measure the scripts folder using the default size of bytes.
.EXAMPLE
PS C:\> dir c:\scripts -Directory | measure-folder -Unit kb | Sort Size* -Descending | Select -first 5 | format-table -AutoSize
Path Name Count SizeKB
---- ---- ----- ------
C:\scripts\GP GP 40 2287.08
C:\scripts\Workflow Workflow 64 1253.02
C:\scripts\modhelp modhelp 1 386.49
C:\scripts\stuff stuff 4 309.09
C:\scripts\ADTFM-Scripts ADTFM-Scripts 76 297.78
Get all the child folders under C:\scripts, measuring the size in KB. Sort the results on the size property in descending order. Then select the first 5 objects and format the results as a table.
.EXAMPLE
PS C:\> measure-folder $env:temp -Average -unit MB -round 10
Path : C:\Users\Jeff\AppData\Local\Temp
Name : Temp
Count : 64626
SizeMB : 6769.94603252411
AvgMB : 0.104755764437287
Measure all the %TEMP% folder, including a file average all formatted in MB with no rounding
.NOTES
Last Updated: July 22, 2015
Version : 2.2
Originally published at https://jdhitsolutions.com/blog/scripting/3715/friday-fun-the-measure-of-a-folder/
Learn more about PowerShell:
Essential PowerShell Learning Resources
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.LINK
Get-ChildItem
Measure-Object
.INPUTS
string or directory
.OUTPUTS
Custom object
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[ValidateScript({
if (Test-Path $_) {
$True
}
else {
Throw "Cannot validate path $_"
}
})]
[Alias("fullname")]
[string]$Path = ".",
[switch]$NoRecurse,
[Alias("avg")]
[switch]$Average,
[ValidateSet("bytes","KB","MB","GB","TB","PB")]
[string]$Unit = "bytes",
[ValidateRange(0,10)]
[int]$Round = 2
)
Begin {
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
#hash table of parameters to Splat to Get-ChildItem
$dirHash = @{
File = $True
Recurse = $True
}
if ($NoRecurse) {
Write-Verbose "No recurse"
$dirHash.remove("recurse")
}
#hash table of parameters to splat to measure-Object
$measureHash = @{
Property = "length"
Sum = $True
}
if ($Average) {
Write-Verbose "Including Average"
$measureHash.Add("Average",$True)
}
Write-Verbose "Rounding to $Round decimal points."
} #begin
Process {
$Resolved = Resolve-Path -Path $path
$Name = Split-Path -Path $Resolved -Leaf
#verify we are in the file system
if ($Resolved.Provider.Name -eq 'FileSystem') {
#define a hash table to hold new object properties
$propHash = [ordered]@{
Path=$Resolved.Path
Name=$Name
}
Write-Verbose "Measuring $resolved in $unit"
$dirHash.Path = $Resolved
$stats = Get-ChildItem @dirHash | Measure-Object @measureHash
Write-Verbose "Measured $($stats.count) files"
$propHash.Add("Count",$stats.count)
$unitHash = @{
Bytes = 1
KB = 1KB
MB = 1MB
GB = 1GB
TB = 1TB
PB = 1PB
}
$value = [Math]::Round($stats.sum/$UnitHash.item($unit),$Round)
$Label = "Size"
if ($unit -ne 'bytes') {
$label+= $($unit.ToUpper())
}
$propHash.Add($label,$value)
#repeat process for Average
if ($Average) {
$value = [Math]::Round($stats.average/$UnitHash.item($unit),$Round)
$Label = "Avg"
if ($unit -ne 'bytes') {
$label+= $($unit.ToUpper())
}
$propHash.Add($label,$value)
} #if Average
#write the new object to the pipeline
New-Object -TypeName PSobject -Property $propHash
}
else {
Write-Warning "You must specify a file system path."
}
} #process
End {
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end
} #end function
添加了舍入选项后,结果是相同的。
我发誓这是最后一次改变。除非有人给我一个好主意!享受。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag