名前 Where-Object 概要 Selects objects from a collection based on their property values. 構文 Where-Object [-Property] [[-Value] []] [-EQ] [-InputObject []] [] Where-Object [-Property] [[-Value] []] [-InputObject []] -Contains [] Where-Object [-FilterScript] [-InputObject []] [] Where-Object [-Property] [[-Value] []] [-InputObject []] -GE [ ] Where-Object [-Property] [[-Value] []] [-InputObject []] -GT [ ] Where-Object [-Property] [[-Value] []] [-InputObject []] -In [ ] Where-Object [-Property] [[-Value] []] [-InputObject []] -CContains [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CEQ [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CGE [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CGT [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CIn [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CLE [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CLike [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CLT [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CMatch [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CNE [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CNotContains [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CNotIn [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CNotLike [] Where-Object [-Property] [[-Value] []] [-InputObject []] -CNotMatch [] Where-Object [-Property] [[-Value] []] [-InputObject []] -Is [ ] Where-Object [-Property] [[-Value] []] [-InputObject []] -IsNot [] Where-Object [-Property] [[-Value] []] [-InputObject []] -LE [ ] Where-Object [-Property] [[-Value] []] [-InputObject []] -Like [] Where-Object [-Property] [[-Value] []] [-InputObject []] -LT [ ] Where-Object [-Property] [[-Value] []] [-InputObject []] -Match [] Where-Object [-Property] [[-Value] []] [-InputObject []] -NE [ ] Where-Object [-Property] [[-Value] []] [-InputObject []] -NotContains [] Where-Object [-Property] [[-Value] []] [-InputObject []] -NotIn [] Where-Object [-Property] [[-Value] []] [-InputObject []] -NotLike [] Where-Object [-Property] [[-Value] []] [-InputObject []] -NotMatch [] 説明 The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example you can use the Where-Object cmdlet to select files that were created a fter a certain date, events with a particular ID, or computers with a particular version of Windows. Beginning in Windows PowerShell 3.0, there are two different ways to construct a Where-Object command. Script block. You can use a script block to specify the property name, a comparison operator, and a propert y value. Where-Object returns all objects for which the script block statement is true. For example, the following command gets processes in the Normal priority class, that is, processes where th e value of the PriorityClass property equals "Normal". Get-Process | Where-Object {$_.PriorityClass -eq "Normal"} All Windows PowerShell comparison operators are valid in the script block format. For more information abou t comparison operators, see about_Comparison_Operators (http://go.microsoft.com/fwlink/?LinkID=113217). Comparison statement. You can also write a comparison statement, which is much more like natural language. Comparison statements were introduced in Windows PowerShell 3.0. For example, the following commands also get processes that have a priority class of "Normal". These comman ds are equivalent and can be used interchangeably. Get-Process | Where-Object -Property PriorityClass -eq -Value "Normal" Get-Process | Where-Object PriorityClass -eq "Normal" Beginning in Windows PowerShell 3.0, Where-Object adds comparison operators as parameters in a Where-Object command. Unless specified, all operators are case-insensitive. Prior to Windows PowerShell 3.0, the compar ison operators in the Windows PowerShell language could be used only in script blocks. パラメーター -Contains Specifies the Contains operator, which gets objects when any item in the property value of the object i s an exact match for the specified value. For example: Get-Process | where ProcessName -contains "Svchost" If the property value contains a single object, Windows PowerShell converts it to a collection of one o bject. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -EQ [] Specifies the equals operator, which gets objects when the property value is the same as the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 false 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -FilterScript Specifies the script block that is used to filter the objects. Enclose the script block in braces ( {} ). The parameter name (-FilterScript) is optional. 必須 true 位置 1 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -GE Specifies the Greater-than-or-equal operator, which gets objects when the property value is greater tha n or equal to the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -GT Specifies the Greater-than operator, which gets objects when the property value is greater than the spe cified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -In Specifies the In operator, which gets objects when the property value matches any of the specified valu es. For example: Get-Process | where -Property ProcessName -in -Value "Svchost", "TaskHost", "WsmProvHost" If the value of the Value parameter is a single object, Windows PowerShell converts it to a collection of one object. If the property value of an object is an array, Windows PowerShell uses reference equality to determine a match. Where-Object returns the object only if the value of the Property parameter and any value of the Value parameter are the same instance of an object. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -InputObject [] Specifies the objects to be filtered. You can also pipe the objects to Where-Object. When you use the I nputObject parameter with Where-Object, instead of piping command results to Where-Object, the InputObj ect value―even if the value is a collection that is the result of a command, such as InputObject (Get- Process)―is treated as a single object. Because InputObject cannot return individual properties from an array or collection of objects, it is recommended that if you use Where-Object to filter a collection of objects for those objects that have specific values in defined properties, you use Where-Object in t he pipeline, as shown in the examples in this topic. 必須 false 位置 named 既定値 none パイプライン入力を許可する true (ByValue) ワイルドカード文字を許可する false -Is Specifies the Is operator, which gets objects when the property value is an instance of the specified . NET Framework type. Enclose the type name in square brackets. For example, Get-Process | where StartTime -Is [DateTime] This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -IsNot Specifies the Is-Not operator, which gets objects when the property value is not an instance of the spe cified .NET Framework type. For example, Get-Process | where StartTime -IsNot [System.String] This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -LE Specifies the Less-than-or-equals operator. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -LT Specifies the Less-than operator, which gets objects when the property value is less than the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -Like Specifies the Like operator, which gets objects when the property value matches a value that includes w ildcard characters. For example: Get-Process | where ProcessName -like "*host" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -Match Specifies the Match operator, which gets objects when the property value matches the specified regular expression. When the input is scalar, the matched value is saved in $Matches automatic variable. For example: Get-Process | where ProcessName -match "shell" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -NE Specifies the Not-equals operator, which gets objects when the property value is different than the spe cified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -NotContains Specifies the Not-Contains operator, which gets objects when none of the items in the property value is an exact match for the specified value. For example: Get-Process | where ProcessName -NotContains "Svchost" "NotContains" refers to a collection of values and is true when the collection does not contain any ite ms that are an exact match for the specified value. If the input is a single object, Windows PowerShel l converts it to a collection of one object. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -NotIn Specifies the Not-In operator, which gets objects when the property value is not an exact match for an y of the specified values. For example: Get-Process | where -Value "svchost" -NotIn -Property ProcessName If the value of the Value parameter is a single object, Windows PowerShell converts it to a collection of one object. If the property value of an object is an array, Windows PowerShell uses reference equality to determine a match. Where-Object returns the object only if the value of the Property parameter and any value of the Value parameter are not the same instance of an object. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -NotLike Specifies the Not-Like operator, which gets objects when the property value does not match a value that includes wildcard characters. For example: Get-Process | where ProcessName -NotLike "*host" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -NotMatch Specifies the not-match operator, which gets objects when the property value does not match the specifi ed regular expression. When the input is scalar, the matched value is saved in $Matches automatic varia ble. For example: Get-Process | where ProcessName -NotMatch "PowerShell" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -Property Specifies the name of an object property. The parameter name (-Property) is optional. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 1 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -Value [] Specifies a property value. The parameter name (-Value) is optional. This parameter is introduced in Windows PowerShell 3.0. 必須 false 位置 2 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CContains Specifies the case-sensitive Contains operator, which gets objects from a collection when the property value of the object is an exact match for the specified value For example: Get-Process | where ProcessName -contains "svchost" "Contains" refers to a collection of values and is true when the collection contains an item that is an exact match for the specified value. If the input is a single object, Windows PowerShell converts it to a collection of one object. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CEQ Specifies the case-sensitive Equals operator, which gets objects when the property value is the same as the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CGE Specifies the case-sensitive Greater-than-or-equal value, which gets objects when the property value is greater than or equal to the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CGT Specifies the case-sensitive Greater-than property, which gets objects when the property value is great er than the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CIn Specifies the case-sensitive In operator, which gets objects when the property value includes the speci fied value. For example: Get-Process | where -Value "svchost" -CIn ProcessName The In operator is much like the Contains operator, except that the property and value positions are re versed. For example, the following statements are both true. "abc", "def" -CContains "abc" "abc" -CIn "abc", "def" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CLE Specifies the case-sensitive Less-than-or-equal operator, which gets objects when the property value is less-than or equal to the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CLT Specifies the case-sensitive Less-than operator, which gets objects when the property value is less-tha n the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CLike Specifies the case-sensitive Like operator, which gets objects when the property value matches a value that includes wildcard characters. For example: Get-Process | where ProcessName -CLike "*host" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CMatch Specifies the case-sensitive Match operator, which gets objects when the property value matches the spe cified regular expression. When the input is scalar, the matched value is saved in $Matches automatic v ariable. For example: Get-Process | where ProcessName -CMatch "Shell" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CNE Specifies the case-sensitive Not-Equals operator, which gets objects when the property value is differe nt than the specified value. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CNotContains Specifies the case-sensitive Not-Contains operator, which gets objects when the property value of the o bject is not an exact match for the specified value. For example: Get-Process | where ProcessName -CNotContains "svchost" "NotContains" and "CNotContains refer to a collection of values and are true when the collection does n ot contains any items that are an exact match for the specified value. If the input is a single object, Windows PowerShell converts it to a collection of one object. This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CNotIn Specifies the case-sensitive Not-In operator, which gets objects when the property value is not an exac t match for the specified value. For example: Get-Process | where -Value "svchost" -CNotIn -Property ProcessName The Not-In and CNot-In operators are much like the Not-Contains and CNot-Contains operators, except th at the property and value positions are reversed. For example, the following statements are true. "abc", "def" -CNotContains "Abc" "abc" -CNotIn "Abc", "def" 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CNotLike Specifies the case-sensitive Not-Like operator, which gets objects when the property value does not mat ch a value that includes wildcard characters. For example: Get-Process | where ProcessName -CNotLike "*host" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false -CNotMatch Specifies the case-sensitive Not-match operator, which gets objects when the property value does not ma tch the specified regular expression. When the input is scalar, the matched value is saved in $Matches automatic variable. For example: Get-Process | where ProcessName -CNotMatch "Shell" This parameter is introduced in Windows PowerShell 3.0. 必須 true 位置 named 既定値 none パイプライン入力を許可する false ワイルドカード文字を許可する false このコマンドレットは、次の共通パラメーターをサポートします: Verbose、 Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、 OutBuffer, PipelineVariable、および OutVariable。詳細については、 about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216) を参照してください。 入力 System.Management.Automation.PSObject You can pipe the objects to be filtered to Where-Object. 出力 Object Where-Object returns selected items from the input object set. メモ Beginning in Windows PowerShell 4.0, Where() operator behavior has changed. Collection.Where('property match name') no longer accepts string expressions in the format "Property CompareOperator Value". How ever, the Where() operator accepts string expressions in the format of a scriptblock; this is still sup ported. The following examples show the behavior that has changed. The following two examples show Where() object behavior that is no longer supported. (Get-Process).Where('ProcessName match PowerShell') (Get-Process).Where('ProcessName match PowerShell', 'Last', 1) The following three examples show Where() object behavior that is supported in Windows PowerShell 4.0 a nd forward. (Get-Process).Where({$_.ProcessName match "PowerShell"}) (Get-Process).Where{$_.ProcessName match "PowerShell"} (Get-Process).Where({$_.ProcessName match "PowerShell"}, ‘Last’, 1) -------------------------- EXAMPLE 1 -------------------------- PS C:\>Get-Service | Where-Object {$_.Status -eq "Stopped"} PS C:\>Get-Service | where Status -eq "Stopped" This command gets a list of all services that are currently stopped. The "$_" symbol represents each object that is passed to the Where-Object cmdlet. The first command uses the script block format. The second command uses the comparison statement format. Th e commands are equivalent and can be used interchangeably. -------------------------- EXAMPLE 2 -------------------------- PS C:\>Get-Process | Where-Object {$_.WorkingSet -gt 25000*1024} PS C:\>Get-Process | Where-Object WorkingSet -gt (25000*1024) This command lists processes that have a working set greater than 25,000 kilobytes (KB). Because the value of the WorkingSet property is stored in bytes, the value of 25,000 is multiplied by 1,024. The first command uses the script block format. The second command uses the comparison statement format. Th e commands are equivalent and can be used interchangeably. -------------------------- EXAMPLE 4 -------------------------- PS C:\>Get-Process | Where-Object {$_.ProcessName -Match "^p.*"} PS C:\>Get-Process | Where-Object ProcessName -Match "^p.*" This command gets the processes that have a ProcessName property value that begins with the letter "p". The match operator lets you use regular expression matches. The first command uses the script block format. The second command uses the comparison statement format. Th e commands are equivalent and can be used interchangeably. -------------------------- EXAMPLE 5 -------------------------- PS C:\>Get-Process | Where-Object -Property Handles -ge -Value 1000 PS C:\>Get-Process | where Handles -ge 1000 This example shows how to use the new comparison statement format of the Where-Object cmdlet. The first command uses the comparison statement format. In this command, no aliases are used and all parame ters include the parameter name. The second command is the more natural use of the comparison command format. The "where" alias is substitut ed for the "Where-Object" cmdlet name and all optional parameter names are omitted. -------------------------- EXAMPLE 6 -------------------------- The first pair of commands gets commands that have any value for the OutputType property of the command. Th ey omit commands that do not have an OutputType property and those that have an OutputType property, but no property value. PS C:\>Get-Command | where OutputType PS C:\>Get-Command | where {$_.OutputType} The second pair of commands gets objects that are containers. It gets objects that have the PSIsContainer p roperty with a value of True ($true) and excludes all others.The "equals $True" (-eq $true) part of the com mand is assumed by the language. You do not need to specify it explicitly. PS C:\>Get-ChildItem | where PSIsContainer PS C:\>Get-ChildItem | where {$_.PSIsContainer} The third pair of commands uses the Not operator (!) to get objects that are not containers. It gets object s that do have the PSIsContainer property and those that have a value of False ($false) for the PSIsContain er property.You cannot use the Not operator (!) in the comparison statement format of the command. PS C:\>Get-ChildItem | where {!$_.PSIsContainer} PS C:\>Get-ChildItem | where PSIsContainer -eq $false This example shows how to write commands that return items that are true or false or have any value for a s pecified property. The example shows both the script block and comparison statement formats for the command . -------------------------- EXAMPLE 7 -------------------------- PS C:\>Get-Module -ListAvailable | where {($_.Name -notlike "Microsoft*" -and $_.Name -notlike "PS*") -and $_.HelpInfoUri} This example shows how to create a Where-Object command with multiple conditions. This command gets non-core modules that support the Updatable Help feature. The command uses the ListAvaila ble parameter of the Get-Module cmdlet to get all modules on the computer. A pipeline operator sends the mo dules to the Where-Object cmdlet, which gets modules whose names do not begin with "Microsoft" or "PS" and have a value for the HelpInfoURI property, which tells Windows PowerShell where to find updated help files for the module. The comparison statements are connected by the -And logical operator. The example uses the script block command format. Logical operators, such as -And and -Or, are valid only i n script blocks. You cannot use them in the comparison statement format of a Where-Object command. For more information about Windows PowerShell logical operators, see about_Logical_Operators (http://go.mic rosoft.com/fwlink/?LinkID=113238). For more information about the Updatable Help feature, see about_Updatab le_Help (http://go.microsoft.com/fwlink/?LinkID=235801). 関連するリンク Online Version: http://go.microsoft.com/fwlink/p/?linkid=289623 Compare-Object ForEach-Object Group-Object Measure-Object New-Object Select-Object Sort-Object Tee-Object