Where-Object

 

Additional Resources for Where-Object

 

Filtering Returned Data

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/where-object.mspx

 

 

SYNOPSIS

Creates a filter that controls which objects will be passed along a command pipeline.

 

SYNTAX

Where-Object [-filterScript] <scriptblock> [-inputObject <psobject>] [<CommonParameters>]

 

DETAILED DESCRIPTION

Creates a filter that controls which objects will be passed along a command pipeline. It filters objects passed to it as pipelined input or objects provided as the value of the InputObject parameter. It determines which objects to pass along the pipeline by evaluating a script block that may include a reference to an object being filtered. If the result of the evaluation is True, the object being processed is passed along the pipeline; otherwise, the object is discarded.

 

PARAMETERS

 

-filterScript <scriptblock>

Specifies the script block to evaluate in determining which input objects will be passed along the command pipeline.

 

Required?

true

Position?

1

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-inputObject <psobject>

Specifies the objects to be filtered. If you save the output of a command in a variable, you can use InputObject to pass the variable to Where-Object. However, typically, the InputObject parameter is not typed in the command. Instead, when you pass an object through the pipeline, Windows PowerShell associates the passed object with the InputObject parameter.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

true (ByValue)

Accept wildcard characters? 

false

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

NOTES

 

For more information, type "Get-Help Where-Object -detailed". For technical information, type "Get-Help Write-Object -full".

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

EXAMPLE 1

 

get-service | where-object {$_.Status -eq "Stopped"}

 

This command gets a list of all services that are currently stopped.

 

EXAMPLE 2

 

get-process | where-object {$_.workingset -gt 25000*1024}

 

This command lists processes that have a working set greater than  25000K. The value of the workingset property is stored in bytes, so the 25000 is multiplied by 1024.

 

EXAMPLE 3

 

get-process | where-object { $_.ProcessName -match "^p.*" }

 

This command gets the processes with a ProcessName property that begins with a letter p. The match operator enables you to use regular expressions within a where clause.

 

EXAMPLE 4

 

get-process -name svchost | where-object {$True}

 

This command lists all of the processes named svchost. The where-object cmdlet evaluates the script block, which typically includes a reference to the object currently in the pipeline ($_),  and casts the results to a boolean type: True or False. If the result is True, the object is passed along the pipeline, otherwise it is discarded. In this case, the script block just returns True, so all objects are passed along the pipeline.

 

RELATED LINKS

Select-Object

about_where

about_regular_expression