Filter input from the pipeline, control which objects will be passed along the command pipeline.
The '?' symbol and Where are both aliases for Where-Object.
Syntax Where-Object [-filterScript] {scriptblock} [-inputObject psobject] [CommonParameters] Key -FilterScript scriptblock An expression that resolves to a Boolean (TRUE/FALSE) value. This will determine which input objects will be passed along the command pipeline. -inputObject psobject The objects to be filtered. Typically objects are passed through the pipeline. When the -InputObject parameter is used to submit a collection of items, Where-Object receives one object that represents the collection. Because one collection object cannot be filtered, Where-Object returns the entire collection unchanged. To convert multiple items, pipe them to Where-Object. This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its direct use with arrays (collections) does not (yet) provide any useful functionality.
The '?' symbol and Where are both aliases for Where-Object.
In some cases the .Where method may be faster.
Where-object 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.
The scriptblock expression can use any of the PowerShell comparison operators as long as the result is a boolean.
Also: -not logical not (with ! as an alias)
and -xor (Exclusive OR)
A comparison statement, is a simplified syntax that can be used to filter the pipeline, neither the {brackets} or the pipeline placeholder $_ are required. Available in PowerShell 3.0 and greater.
Syntax command | Where test1 [conjunction test2] Key conjunction Any of the following: (logical boolean operators) -and, -or (Logical or), -bor (Bitwise or), -band (Bitwise and), -xor Test1 An expression that resolves to a Boolean (TRUE/FALSE) value. Test2 An expression that resolves to a Boolean (TRUE/FALSE) value. comparison operatorsAs above, the expression can use any PowerShell comparison operators as long as the result is a boolean.
Also: -not logical Not (with ! as an alias) and -xor (Exclusive OR)This only works with a single object property, and at most a single comparison.
Anything more complex still needs to be enclosed in a script block.For example - to list files on drive f: greater than 1000000 bytes:
PS C:\> Get-ChildItem f:\ | where Length -gt 1000000Which is equivalent to this scriptblock:
PS C:\> Get-ChildItem f:\ | where {$_.Length -gt 1000000}You can also read properties in a smilar way:
PS C:\> (Get-ChildItem f:\).LengthYou can’t use this simplified syntax to set properties.
To filter items that have a property set (not empty) just include the property name:
PS C:\> Get-ADuser -Filter * | where DisplayName
Get a list of files but exclude folders:
PS C:\> Get-ChildItem 'C:\Apps\' -Recurse | Where-Object {-not $_.PsIsContainer}
Get a list of all services that are currently stopped:
PS C:\> Get-Service | Where-Object {$_.Status -eq 'Stopped'}
Lists the processes with a working set greater than 25000K. (bytes = Kilobytes*1024):
PS C:\> Get-process | ? {$_.workingset -gt 25000*1024}
Get the processes with a ProcessName property that begins with the letter p. The -match operator enables you to use regular expressions:
PS C:\> Get-process | Where-Object { $_.ProcessName -match '^p.*' }
“The enemy of art is the absence of limitations” ~ Orson Welles
PowerShell Syntax - Regular Expressions
ForEach-Object - Loop for each object in the pipeline.
Get-ChildItem - Get child items (contents of a folder or registry key).
Group-Object - Group the objects that contain the same value for a common property.
Select-Object - Select objects based on parameters set in the Cmdlet command string.
Sort-Object - Sort the input objects by property value.
Where (method) - Filter input from a collection.