返回列表 发帖

[原创教程] PowerShell 技能连载 - 用 Group-Object 来创建哈希表

原始链接:PowerShell 技能连载 - 用 Group-Object 来创建哈希表
发表日期:2014-08-12


适用于所有 PowerShell 版本

`Group-Object` 能把对象输送到管道中,然后在一个管道中把属性相同的对象排在一起。

这个功能十分有用,特别是当您用 `Group-Object` 来返回哈希表时。它将生成一个按服务状态分组的哈希表:
$hash = Get-Service |
  Group-Object -Property Status -AsHashTable -AsStringCOPY
$hash.Running
    $hash.Stopped

可以用任何想要的属性来分组。这个例子将用三个组来分组文件:一组为小文件,一个组为中等文件,另一个组位大文件。
$code =
{
  if ($_.Length -gt 1MB)
  {'huge'}
  elseif ($_.Length -gt 10KB)
  {'average'}
  else
  {'tiny'}
}
$hash = Get-ChildItem -Path c:\windows |
  Group-Object -Property $code -AsHashTable -AsString
#$hash.Tiny
$hash.HugeCOPY
本文国际来源:Use Group-Object to Create Hash Tables
PowerShell 群:271143343

返回列表