Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?{} mean in PowerShell?

Tags:

powershell

What does a ?{ } block mean in a PowerShell?

For example

[enum]::GetValues([io.fileoptions]) | ?{$_.value__ -band 0x90000000}
like image 533
leeand00 Avatar asked Aug 10 '16 12:08

leeand00


3 Answers

? is an alias for Where-Object. The curly brackets are used if you have to do something more complex with the actual object. You could also write:

[enum]::GetValues([io.fileoptions]) | Where-Object { $_.value__ -band 0x90000000}
like image 122
Martin Brandl Avatar answered Nov 01 '22 22:11

Martin Brandl


Use Get-Alias cmdlet in doubts:

Description

     The Get-Alias cmdlet gets the aliases (alternate names for commands and executable files) in the current session. This includes built-in aliases, aliases that you have set or imported, and aliases that you have added to your Windows PowerShell profile.

     By default, Get-Alias takes an alias and returns the command name. When you use the Definition parameter, Get-Alias takes a command name and returns its aliases.

     Beginning in Windows PowerShell 3.0, Get-Alias displays non-hyphenated alias names in an "<alias> -> <definition>" format to make it even easier to find the information that you need.

PS D:\PShell> Get-Alias ?

CommandType     Name                                               ModuleName  
-----------     ----                                               ----------  
Alias           % -> ForEach-Object                                            
Alias           ? -> Where-Object                                              
Alias           h -> Get-History                                               
Alias           r -> Invoke-History                                            
like image 25
JosefZ Avatar answered Nov 01 '22 22:11

JosefZ


? is an alias for Where-Object cmdlet, though it also has another alias - where.

{} curly brackets are used in case of script block, in this case it's a filter script block, it's mostly used for complex filtering, i.e. for more than one critera, like this:

Get-Service | Where-Object -FilterScript {$_.Name -like '*audio*' -and $_.Status -eq 'Running'}
like image 7
Kirill Pashkov Avatar answered Nov 01 '22 21:11

Kirill Pashkov