当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell:测量对象(计数、平均值、总和……)

作者:精品下载站 日期:2024-12-14 05:31:51 浏览:12 分类:玩电脑

PowerShell:测量对象(计数、平均值、总和……)


Measure-Object cmdlet 对对象进行计数。但它还可以做得更多。我们可以计算总和、平均值等等。在这篇博文中,我展示了一些使用 Measure-Object 的示例。让我们深入了解一下。

首先,我们可以在这里调用Measure-Object的帮助:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object?view=powershell-7.4

现在我准备了几个例子。我建议将这些示例复制到 ISE 或 VS Code 中并在那里执行它们,以了解此 cmdlet 的机制和可能性。

开始了:

# Measure-Object

$numbers = 1,2,3,4,5,6,7,8,9,10
$numbers | Measure-Object -Average -Maximum -Minimum -Sum

$array = "apple", "banana", "cherry"
$array | Measure-Object

$array = "apple", "banana", "cherry"
$array | Measure-Object -Maximum -Property Length

$text = "Hello, world!"
$text | Measure-Object -Line -Word -Character

"One", "Two", "Three", "Four" | Set-Content -Path $home\tmp.txt
Get-Content $home\tmp.txt | Measure-Object -Character -Line -Word


# Measure-Object in Real-World Scenarios

# Count Commands
Get-Command | Measure-Object | Select-Object -Property Count
# Cound ADUsers
Get-ADUser -Filter 'LogonCount -eq "0"' -Properties LogonCount | Measure-Object 
# Count Sum of Files in a Folder and Subfolders
(Get-ChildItem -Path "C:\Users\PatrickGruenauersid-\SynologyDrive\Videos\Udemy" -Recurse -Include *.mp4 | 
Measure-Object Length -Sum).Sum /1GB

我希望您能够从这篇博文中学到一些可以付诸实践的东西。下次见!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯