IT Questions and Answers :)

Thursday, November 7, 2019

Which of the examples work in PowerShell version 3, but not in PowerShell version 2?

Which of the examples work in PowerShell version 3, but not in PowerShell version 2?

  • $collection | where $_.property -eq 'test'
  • $collection.where({$_.property -eq 'test'})
  • $collection | where property -eq 'test'
  • $collection | where {$_.property -eq 'test'} 

EXPLANATION

PowerShell v2 and v3 Differences
Where-Object no longer requires braces (source)

PS v2.0:
Get-Service | Where { $_.Status -eq 'running' }

PS v3.0:
Get-Service | Where Status -eq 'running'

PS V2.0 Error Message:
    Where : Cannot bind parameter ‘FilterScript’. Cannot convert the “[PropertyName]” value of the type “[Type]” to type “System.Management.Automation.ScriptBlock”.
 

Share:

0 comments:

Post a Comment

Popular Posts