Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the PowerShell phrase " | ? {...}" mean?

Tags:

powershell

There are a couple of syntactic idiosyncrasies that PowerShell has that I can't find much documentation on. Today my question is the ? {...}

example

PS> Get-SPServiceInstance | ? {$_.GetType.toString() -eq $varname}

I understand what the pipe means. I'm pretty sure $_ is used to reference the current item in some kind of loop.

Can someone explain what this means? Also it would be great if someone could point me to a place where I can find out about PowerShell reserved symbols.

like image 318
StaticMethod Avatar asked Jul 26 '26 23:07

StaticMethod


2 Answers

[^_^]16:46:21[1]>get-help ?

Name                              Category  Synopsis
----                              --------  --------
%                                 Alias     ForEach-Object
?                                 Alias     Where-Object
h                                 Alias     Get-History
r                                 Alias     Invoke-History

"?" is an alias for "where-object"

like image 151
dance2die Avatar answered Jul 28 '26 14:07

dance2die


'?' is an alias to the Where-Object cmdlet. Where-Object takes a scriptblock (e.g '{...}') and evaluates its code. If the code evaluates to $true, the current object is written to the pipeline and is available to the next command in chain, otherwise ($false) the object is discarded.

For more help type:

Get-Help Where-Object -Full
like image 22
Shay Levy Avatar answered Jul 28 '26 13:07

Shay Levy