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

[玩转系统] PowerShell 计数 | PowerShell 计数快速浏览 |例子

作者:精品下载站 日期:2024-12-14 04:54:02 浏览:15 分类:玩电脑

PowerShell 计数 | PowerShell 计数快速浏览 |例子


[玩转系统] PowerShell 计数 | PowerShell 计数快速浏览 |例子

PowerShell 计数简介

以下文章提供了 PowerShell 计数的概述。在我们的日常活动中,有很多场景需要我们去了解计数。它可能是任何内容,从某个位置保存的文件数量到活动和非活动用户的数量、一天报告的错误消息数量、早于某个日期的文件等等。在任何这些实例中,都可以使用 Measure-Object 或 count 方法获得计数。

Hadoop、数据科学、统计及其他

测量对象的语法

measure-object cmdlet 计算文件中的行数、字符数和单词数,以及对象的数字属性。它可以根据 cmdlet 中可用的参数执行三种类型的计算。它可以计算特定类别的事物或物体的数量。它可以执行算术运算,例如最小值、最大值和平均值。如果对象是字符串值,则可以计算字符数或单词数。

示例:

代码:

Get-ChildItem -Path C:\vignesh\Test | Measure-Object -Property LastAccessTime

输出:

[玩转系统] PowerShell 计数 | PowerShell 计数快速浏览 |例子

参数:

  • Allstate:这表示上述属性的所有数据。该参数的数据类型为switch参数,默认值为none。它不接受管道输入,也未指定通配符。
  • 平均值:此参数计算上述属性的平均值。它的类型是开关参数。默认值为无。它不接受管道输入,也未指定通配符。
  • 字符:此参数返回指定输入对象中的字符数。它的类型是开关参数。默认值为无。它不接受管道输入,也未指定通配符。
  • IgnoreWhiteSpace:为了避免使用此参数,默认情况下确定字符数时会考虑空格。其类型为开关。默认值为无。它不接受管道输入,也未指定通配符。
  • 输入对象:表示测量项目。提供此参数时,输入对象将作为单个实体进行处理,而不是提供给管道。 PSObject 是它的类型。无是默认值。接受管道输入,但不接受通配符。
  • 行:这将返回指定输入中的行数。它的类型是开关参数。默认值为无。它不接受管道输入,并且禁止使用通配符。
  • 最大值:这将返回上述属性或对象的最大值。它的类型是开关参数。默认值为无。
  • 最小值:这将返回上述属性或对象的最小值。它的类型是开关参数。默认值为无。它不接受管道输入,并且禁止使用通配符。
  • 属性:这表示要测量的指定属性。如果未指定属性,则返回所有属性。数据类型为 PSPropertyExpression[]。默认值为无。
  • 标准偏差:这将返回上述属性或对象的平均偏差值。它的类型是开关参数。默认值为无。它不接受管道输入,并且禁止使用通配符。
  • 总和:这将返回属性或对象的总价值。它的类型是开关参数。默认值为无。它不接受管道输入,也不允许通配符。
  • 单词:这将返回输入中的单词总数。它的类型是开关参数。默认值为无。它不接受管道输入,也不允许通配符。

PowerShell 计数示例

下面给出了提到的示例:

代码:

Write-Host "Welcome to powershell count demo"
Write-Host "number of files in a folder"
(Get-ChildItem -Path C:\vignesh -Recurse | Where-Object{ $_}).count
Write-Host "to count the total number of folders and subfolders in a path"
(Get-ChildItem -Path C:\vignesh -Recurse | Where-Object{ $_.PSIsContainer }).count
Write-Host "Number of warning in event viewer for the past 48 hours"
(Get-EventLog -LogName System -After ((Get-Date).AddDays(-2)) | Where-Object {$_.EntryType -eq "Warning"}).count
Write-Host "count using measure object command demo"
(Get-ChildItem -Path "C:\vignesh\test" | Measure-Object | Select-Object Count).count
Write-Host "number of files using measure object"
Get-ChildItem -Path C:\vignesh\Test -Recurse | Where-Object{ !($_.PSIsContainer) } | Measure-Object
Write-Host "Selecting only count"
Get-ChildItem -Path C:\Vignesh\Test\Actual1239 | Measure-Object | Select-Object Count
Write-Host "Number of files in each category"
Get-ChildItem -Path C:\vignesh | Group-Object Extension -NoElement
Write-Host "maximum size of a file in a path"
Get-ChildItem -Path c:\vignesh | Measure-Object -Property length -Maximum
Write-Host "minimum size of a file in a path"
Get-ChildItem -Path c:\vignesh | Measure-Object -Property length -Minimum
Write-Host "average size of a file in a path"
Get-ChildItem -Path c:\vignesh | Measure-Object -Property length -Average
Write-Host "total size of all files in a path"
Get-ChildItem -Path c:\vignesh | Measure-Object -Property length -Sum
Write-Host "number of lines in input file"
Get-ChildItem -Path C:\Vignesh\KB.txt |Measure-Object -Line
Write-Host "number of characters in input file"
Get-ChildItem -Path C:\Vignesh\KB.txt |Measure-Object -Character
Write-Host "number of words in input file"
Get-ChildItem -Path C:\Vignesh\KB.txt |Measure-Object -Word
Write-Host "number of characters in a text file excluding white space"
Get-ChildItem -Path C:\Vignesh\KB.txt |Measure-Object -IgnoreWhiteSpace -Character
Write-Host "counting a csv file"
Import-Csv C:\vignesh\July3.csv | Measure-Object -Property MailboxResolvedOwnerName -Maximum
Import-Csv C:\vignesh\July3.csv | Measure-Object -Property MailboxResolvedOwnerName -Minimum

输出:

[玩转系统] PowerShell 计数 | PowerShell 计数快速浏览 |例子

[玩转系统] PowerShell 计数 | PowerShell 计数快速浏览 |例子

[玩转系统] PowerShell 计数 | PowerShell 计数快速浏览 |例子

[玩转系统] PowerShell 计数 | PowerShell 计数快速浏览 |例子

结论

在本文中,我们探讨了 PowerShell 中计数操作在不同对象上的用法,包括文件对象和其他类型的命令。我们讨论了利用计数对象或测量对象 cmdlet 的各种方法。本文提供了大量示例,展示了如何在各种场景中应用计数。

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

取消回复欢迎 发表评论:

关灯