プロパティのフィルタリング

「filter」プロパティのフィルタリング

「filter」ステートメント(という表現で正しいのかな?)を利用すると、プロパティをフィルタリングできます。


ヘルプは、以下のスクリプトで確認できます。

help about_Filter

指定プロパティ名でフィルタリング

以下を定義しておくと、

filter pick($property)
{
    $_.$property
}

以下のように指定プロパティ名でフィルタリングできます。

MSH C:\> ps | pick name
calc
CCAPP
(中略)

「select-object」と「filter」の違い

オブジェクトのフィルタリング(select-object) - PowerShell Memoで紹介した「select-object」との違いは?


「select-object」はフィルタしたプロパティがコレクションの時に展開しませんが、
「filter」はフィルタしたプロパティがコレクションの時に展開します。


以下に例を示します。

「select-object」はコレクションを展開しない
MSH C:\> get-command | select ParameterSets

ParameterSets
-------------
{__AllParameterSets}
{__AllParameterSets}
{__AllParameterSets}
(省略)
「filter」はコレクションを展開する
MSH C:\> get-command | pick ParameterSets

Parameter Set Name: __AllParameterSets
Is default parameter set: False

  Parameter Name: Value
    Type = System.Object[]
    Position = 1
    IsMandatory = True
    HelpMessage =
    ValueFromPipeline = True
    ValueFromPipelineByPropertyName = True
    ValueFromRemainingArguments = False
    Aliases = {}
    Attributes =
      System.Management.Automation.ParameterAttribute
      System.Management.Automation.AllowNullAttribute
      System.Management.Automation.AllowEmptyCollectionAttribute

  Parameter Name: PassThru
    Type = System.Boolean
    Position = -2147483648
    IsMandatory = False
    HelpMessage =
    ValueFromPipeline = False
    ValueFromPipelineByPropertyName = False
    ValueFromRemainingArguments = False
    Aliases = {}
    Attributes =
      System.Management.Automation.ParameterAttribute
(省略)