BlackHatSamurai
BlackHatSamurai

Reputation: 23483

What does `?{}` Represent in PowerShell

I'm have the following expression in PowerShell:

$oneTypes = Get-ChildItem -Path $Location -Directory


$onlyCompile = @( $BASE_A, $BASE_B, $BASE_C )
#trying figure this line  out
$oneTypes = ($oneTypes | ?{$onlyCompile -contains $_})

I'm not sure what the ? and {...} do here? It looks like it might be a script block but I'm not sure. I'm also wondering how the pipeline comes into play on this.

Upvotes: 0

Views: 121

Answers (2)

Keith Hill
Keith Hill

Reputation: 201622

The answer to the other part of your question is that {...} after the ? or Where does indeed represent a scriptblock. If you look at the help on Where-Object you see this:

-FilterScript <ScriptBlock>
    Specifies the script block that is used to filter the objects. Enclose the script block in braces ( {} ).

    The parameter name (-FilterScript) is optional.

    Required?                    true
    Position?                    1
    Default value
    Accept pipeline input?       false
    Accept wildcard characters?  false

The FilterScript parameter expects a scriptblock. It is positional so you don't have to specify Where -FilterScript { ... }. It is not pipeline bound but the InputObject parameter is pipeline bound. That object gets injected into the scriptblock you provide as $_.

Upvotes: 2

Mitul
Mitul

Reputation: 9854

? is used as a short form for Where-Object command and here it means filter all the objects that are in the supplied array $onlycompile

Upvotes: 5

Related Questions