Group-Object コマンドレットの使用

データのグループ化

Group-Object は優れたコマンドレットです。このコマンドレットを使用すると、指定したプロパティでデータを簡単にグループ化し、結果のカテゴリに含まれるアイテム数をすばやく把握できます。たとえば、次のコマンドは Get-Service コマンドレットを使用して、コンピュータにインストールされているサービスに関する情報を取得し、返されたデータをステータスでグループ化します。

Get-Service | Group-Object status

どういうことでしょうか。よく言われるように、百聞は一見に如かずです。

Count Name                      Group
----- ----                      -----
   56 Running                   {AdobeActiveFileMonitor4.0, ALG, ASChannel, ...
   42 Stopped                   {Alerter, AppMgmt, aspnet_state, Browser...}

次のコマンドは、C:\Scripts フォルダのファイルをファイルの拡張子でグループ化します。

Get-ChildItem c:\scripts | Group-Object extension

このコマンドが返すと予想される種類の情報を次に示します。

Count Name                      Group
----- ----                      -----
    1                           {200}
    2 .gif                      {38DF6AB1-13D4-409C-966D-CBE61F040027.gif, d...
    5 .xls                      {5-15-06.xls, 5-17-06.xls, Book1.xls, invent...
   15 .txt                      {alias.txt, ExcelData.txt, help.txt, methods...
    3 .msh                      {a_new_file.msh, b_new_file.msh, c_new_file....
    6 .zip                      {calculatorv11.zip, IronPython-1.0-Beta2.zip...
    4 .log                      {Employees.log, Employees_NODUPL.log, mylog....
    3 .doc                      {ey.doc, ou.doc, test.txt.doc}
    4 .xml                      {files.xml, my_history.xml, Saved_history.xm...
    1 .txtcls                   {help.txtcls}
    8 .vbs                      {hidden.vbs, imapi.vbs, imapi2.vbs, methods....
    1 .wma                      {HoneyPie.wma}
    2 .htm                      {msh.htm, test.htm}
    1 .csv                      {test.csv}
    1 .ps1                      {test.ps1}
    2 .psc1                     {test.psc1, test.psc1e.psc1}
    1 .jpg                      {welder-small.jpg}

また、各グループのファイル数で情報を並べ替えることもできます。

Get-ChildItem c:\scripts | Group-Object extension | Sort-Object count

結果を Sort-Object にパイプ処理し、Count プロパティについて並べ替えます。返される内容を次に示します。

Count Name                      Group
----- ----                      -----
    1 .csv                      {test.csv}
    1 .ps1                      {test.ps1}
    1 .wma                      {HoneyPie.wma}
    1 .txtcls                   {help.txtcls}
    1 .jpg                      {welder-small.jpg}
    1                           {200}
    2 .htm                      {msh.htm, test.htm}
    2 .gif                      {38DF6AB1-13D4-409C-966D-CBE61F040027.gif, d...
    2 .psc1                     {test.psc1, test.psc1e.psc1}
    3 .msh                      {a_new_file.msh, b_new_file.msh, c_new_file....
    3 .doc                      {ey.doc, ou.doc, test.txt.doc}
    4 .log                      {Employees.log, Employees_NODUPL.log, mylog....
    4 .xml                      {files.xml, my_history.xml, Saved_history.xm...
    5 .xls                      {5-15-06.xls, 5-17-06.xls, Book1.xls, invent...
    6 .zip                      {calculatorv11.zip, IronPython-1.0-Beta2.zip...
    8 .vbs                      {hidden.vbs, imapi.vbs, imapi2.vbs, methods....
   15 .txt                      {alias.txt, ExcelData.txt, help.txt, methods...
Group-Object のエイリアス
  • group

関連リンク