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

[玩转系统] PowerShell 测量对象:带有示例的完整指南

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

PowerShell 测量对象:带有示例的完整指南


计数或计算对象的数字属性(例如字符串对象中的字符、单词和行)可能会令人恼火。幸运的是,PowerShell Measure-Object cmdlet 可以胜任这项任务。

在本教程中,您将了解 Measure-Object cmdlet 在 PowerShell 中自动测量对象属性方面的强大功能。

立即投入以提高您的物体测量技能!

先决条件

本教程是一个动手演示。为了继续进行,您需要一个安装了 PowerShell 的系统。本教程使用带有 PowerShell v7 的 Windows 10。

计算文件和文件夹数量

了解目录中文件和文件夹的数量在各种情况下都会很有帮助。一个例子是当您需要监视文件夹的大小或检查目录中的文件数量时。

Measure-Object cmdlet 计算目录中文件和文件夹的数量。准确的计数可以帮助您更好地管理和优化系统资源。

要了解 Measure-Object cmdlet 如何计算文件和文件夹:

1. 打开 PowerShell,然后运行以下 Get-ChildItem 命令来统计并返回当前目录中的文件和文件夹总数 (Measure-Object)。

Get-ChildItem | Measure-Object

下面的输出指示当前目录中的 4852 个文件和文件夹。

[玩转系统] PowerShell 测量对象:带有示例的完整指南

也许您希望递归地包含目录树中的所有文件和子目录。如果是这样,请附加 -Recurse 参数,如下所示。 Get-ChildItem -Recurse | 测量对象

2. 接下来,运行与第一步相同的命令。但这一次,附加 -Directory 参数以仅计算工作目录中的目录总数。

Get-ChildItem -Directory | Measure-Object

如下所示,总计数现在较低 (135),因为该命令仅计算目录,不包括所有其他对象。

[玩转系统] PowerShell 测量对象:带有示例的完整指南

3. 现在,运行以下命令来统计当前目录中的可执行文件 (.exe)。

-Filter 参数告诉 Get-ChildItem cmdlet 过滤结果并仅返回具有您指定扩展名的文件(即 *.txt、*.jpg*.csv)。

Get-ChildItem -Filter *.exe | Measure-Object

[玩转系统] PowerShell 测量对象:带有示例的完整指南

测量文件大小

测量对象长度和大小可以帮助更好地管理您的系统,因为大文件会影响系统的性能。除了计算属性之外,您还可以通过 Measure-Object cmdlet 测量对象的长度和大小。

运行下面的代码以检索当前目录中的所有文件 (Get-ChildItem) 并默认显示 (Write-Host) 它们各自的长度(以字节为单位)。

下面的代码将每个测量值输出到控制台,包括最小和最大文件大小、平均文件大小、文件大小总和以及文件总数。

# Set the threshold for the average file size
$threshold = 5000000 
# Measure the size of each file in the current directory
$files = Get-ChildItem | Measure-Object -Property length -Minimum -Maximum -Sum -Average

Write-Host "Minimum file size: $($files.Minimum) bytes"
Write-Host "Maximum file size: $($files.Maximum) bytes"
Write-Host "Sum of file sizes: $($files.Sum) bytes"
Write-Host "Average file size: $($files.Average) bytes"
Write-Host "Total number of files: $($files.Count)"

# Check whether the average file size is above or below the threshold
if ($files.Average -ge $threshold) {
    # If the average file size is above the threshold, play a high-pitched sound
    [console]::beep(1000, 500)
} else {
    # If the average file size is below the threshold, play a low-pitched sound
    [console]::beep(500, 500)
}

下面,您可以看到以字节为单位显示的大小。

[玩转系统] PowerShell 测量对象:带有示例的完整指南

也许将代码粘贴到控制台不是你的事。如果是这样,请将代码保存到 PowerShell 脚本文件并运行它。

现在,运行以下命令来获取当前目录中的一个文件,其中 (Where-Object) 的大小 ($_.Length) 等于 (- eq)到最大文件大小($files.Maximum)。

# Get and store the file the same size as the $files.Maximum
$max = Get-ChildItem | Where-Object -FilterScript {$_.Length -eq $files.Maximum}
# Display the value of the maximum size ($files.Maximum) and the file with that size
Write-Host "Maximum file size: $($files.Maximum) bytes `nFile: $max"

下面的输出显示当前目录中最广泛的文件的名称。

[玩转系统] PowerShell 测量对象:带有示例的完整指南

测量文本文件内容

您可能需要确定文本文件中的行数、单词数或字符数,而不是测量文件大小。如果是这样,则 Measure-Object cmdlet 有许多其他参数来测量文本文件内容。

运行以下代码,通过 -Character-Line 测量文本文件的内容 (Measure-Object) -Word,并输出(Write-Host)测量结果。

确保将 $file_path$file_content 变量的值更改为您喜欢的值。

# Define the file path and content
$file_path = "C:\Temp\measure-object.txt"
$file_content = "One fish, two fishes, three fishes"

# Create the file against the defined file path and content
Set-Content -Path $file_path -Value $file_content

# Measure the contents of the text file
$measurements = Get-Content -Path $file_path | Measure-Object -Character -Line -Word

# Output the measurements to the console
Write-Host "The file '$file_path' contains $($measurements.Lines) lines, $($measurements.Words) words, and $($measurements.Characters) characters."

[玩转系统] PowerShell 测量对象:带有示例的完整指南

测量布尔值

布尔值是只能为 true 或 false 的值。在 PowerShell 中,布尔值通常用于控制脚本的流程、确定条件(真或假)或指示命令的成功或失败。

测量布尔值在各种场景中都很有用,例如:

  • 计算数据集中真值或假值的数量。
  • 确定真实值相对于值总数的百分比。

这些信息可以帮助在处理或分析数据时做出正确的决策。

要了解测量布尔值的工作原理:

运行以下命令来计算数组 ($values) 中布尔值(默认为 true 值)的总和。

# Define an array of Boolean values ($true and $false).
$values = $true,$false,$false,$true,$true,$true,$false,$false,$true
# Measure (count) the Boolean values (true values by default).
$values | Measure-Object -Sum

下面,布尔值的总计数为九 (9),而总和为五 (5) )因为只计算真实值。

那么假值呢?跳转到以下步骤,并运行更多命令。

[玩转系统] PowerShell 测量对象:带有示例的完整指南

现在,运行以下代码,从布尔值总数 ($count) 中减去真值总和 ($trueCount)。与方程的差异是错误值的总数 ($falseCount)。

# Get the total count of Boolean values in an array
$count = $values.Count
# Get the sum of true values
$trueCount = ($values | Measure-Object -Sum).Sum
# Subtract the total of true values from the total Boolean values
# to get the total of the false values
$falseCount = $count - $trueCount
# Display total counts of both true and false values
Write-Host "Number of true values: $trueCount"
Write-Host "Number of false values: $falseCount"

[玩转系统] PowerShell 测量对象:带有示例的完整指南

如果测量布尔值是一项日常任务,您可以创建一个可以随时调用的函数来运行该任务。

测量哈希表

PowerShell 中的哈希表(键值对的集合)允许您快速高效地存储和检索数据。如果您需要哈希表中的项目数或最大值和最小值,请让 Measure-Object cmdlet 测量您的哈希表。

运行以下命令在哈希表 ($MyHastable) 中查找 Num 属性的 -Minimum-Maximum 值。

# Create a hashtable containing three hashtables, each with a Num property.
$MyHashtable = @(@{Num=10}, @{Num=20}, @{Num=30})
# Measure the hashtable's Num property by minimum and maximum.
$Stats = $MyHashtable | Measure-Object -Minimum -Maximum Num
# Display the hashtable measurements
$Stats

[玩转系统] PowerShell 测量对象:带有示例的完整指南

测量标准差

检查数据集可能很杂乱,但并非必须如此。在了解数据集值的分布时,测量标准差非常方便。您还可以识别影响结果的任何异常值或异常数据点。

标准差衡量数据集值与平均值的偏差或离散度。该测量值是通过求数据方差的平方根来计算的,数据方差是与平均值的平方差的平均值。

运行下面的代码来计算并显示存储在 $Numbers 变量中的数字数组的 -Average-StandardDeviation

# Define an array of numbers
$Numbers = 10, 20, 30, 40, 50

# Calculate the average and standard deviation of the numbers
$Average = ($Numbers | Measure-Object -Average).Average
$StdDev = ($Numbers | Measure-Object -StandardDeviation).StandardDeviation

# Output the results
"Average: $Average"
"Standard deviation: $StdDev"

在统计学中,平均值衡量集中趋势,描述数据集的典型值或最具代表性的值。

平均值是通过将数据集中的所有数字相加并除以值总数来计算的。在此示例中,数据集为 {10, 20, 30, 40, 50} 总和为 150,该组中有五个数字 (5)。

因此,平均值计算如下:30

Average = (10 + 20 + 30 + 40 + 50) / 5 = 30

另一方面,标准差衡量一组数据的分布或离散度。标准差计算如下:

1. Find the difference between each data point and the average:
   10 - 30 = -20
   20 - 30 = -10
   30 - 30 = 0
   40 - 30 = 10
   50 - 30 = 20
   
2. Square each of these differences:
   (-20)^2 = 400
   (-10)^2 = 100
   0^2 = 0
   10^2 = 100
   20^2 = 400
   
3. Add up all the squared differences:
   400 + 100 + 0 + 100 + 400 = 1000
   
4. Divide the sum of squared differences by the total number of values:
   1000 / 5 = 200
   
5. Take the square root of the result:
   sqrt(200) = 14.142135623731
   
The standard deviation of this set of data is approximately 14.14. But 
in this case, the result is 15.8113883008419 
because of floating-point arithmetic limitations in computers.

[玩转系统] PowerShell 测量对象:带有示例的完整指南

结论

测量数据是一项重要任务,而 PowerShell Measure-Object cmdlet 是完成该任务的强大工具。在本教程中,您学习了如何以多种方式测量数据。无论您正在处理文件、文本文件内容、布尔值、字符串、数组还是哈希表,测量它们都可以提供有价值的见解。

现在,您可以轻松计算数据的平均值、最大值和最小值,并测量数据的变化或分布。借助这些功能,您可以更深入地了解数据,并根据分析做出明智的决策。

有了这些新发现的知识,为什么不扩展您的 PowerShell 工具包并将 Compare-Object 添加到您的武器库中呢?

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

取消回复欢迎 发表评论:

关灯