Late breaking news from PowerShell Conference Europe 2016!!
During PowerShell MVP Chrissy LeMaire’s (@cl) knockout session at PowerShell Conference Europe 2016, “My Journey to 200,000 Rows a Second” (don’t miss the recording; I’ll tweet as soon as it’s available), PowerShell Principal Developer Bruce Payette (@BrucePayette) said that you can call the Where method without parentheses.
“It’s a syntactic twiddle,” he explained, as only Bruce can.
In an exclusive interview, Bruce clarified that, beginning in PowerShell 4.0, you can call ANY method in PowerShell without parentheses if it meets the specified conditions.
The Old Rules: Parentheses are required
Prior to PowerShell 4.0, parentheses were required for any method call, even if the method doesn’t have any arguments, because the PowerShell parser used the parenthesis to identify a method call and distinguish it from a request for a property value.
(Get-Process Notepad).Name ← Property
(Get-Process Notepad).Kill() ← Method (see the trailing parentheses)
The New Rules
Beginning in PowerShell 4.0, you can call ANY method in PowerShell without parentheses if the method meets the following conditions.
- The method takes only one argument
- That argument is a scriptblock
There’s no particular advantage to this other than style, but it’s an interesting stylistic alternative.
This “syntactic twiddle” was designed for the Where and ForEach methods, also introduced in PowerShell 4.0. These magic methods are alternatives to the Where-Object and ForEach-Object cmdlets. They’re faster and have several cool options that make them very valuable. You can read about these new methods here and here.
For example, here is a typical call to the Where method:
$services.Where( {$_.Status -eq 'Running'} )
But, you can also write it like this — without the parentheses. Note that there are no spaces between the “Where” method name and the first curly brace.
$services.Where{$_.Status -eq 'Running'}
Of course, just because you can doesn’t mean that you should. I wouldn’t teach this to beginners, and I probably won’t use it in shared code, but because it’s shown in blogs and you might find it in shared code, it’s important to understand it.
In Other News: Why is Out-Null slow?
Bruce also mentioned that, while piping is always slower than the alternatives…
"Hello" | Write-Output
– is slower than –
Write-Output -InputObject "Hello"
… piping to Out-Null should be as quick as redirecting.
However, in Chrissy’s tests, it was much slower.
$(1..1000) > $null 00.21 ms
$(1..1000) | Out-Null 76.21 ms
“That’s a bug,” Bruce explained. “Under the covers, these two expressions are identical, so they should perform the same. When Jason optimized the first form, he probably forgot to optimize the second form.”
You heard it here! Stay tuned for breaking news on the SAPIEN Technologies blog. ☺
June Blender is a technology evangelist at SAPIEN Technologies, Inc. and a Windows PowerShell MVP. You can reach her at juneb@sapien.com or follow her on Twitter at @juneb_get_help.
Good info.
RT @SAPIENTech: Calling a method without parentheses: Late breaking news from PowerShell Conference Europe 2016!!
https://t.co/bAVsP3sV47
RT @SAPIENTech: Calling a method without parentheses: Late breaking news from PowerShell Conference Europe 2016!!
https://t.co/bAVsP3sV47
RT @SAPIENTech: Calling a method without parentheses: Late breaking news from PowerShell Conference Europe 2016!!
https://t.co/bAVsP3sV47
RT @SAPIENTech: Calling a method without parentheses: Late breaking news from PowerShell Conference Europe 2016!!
https://t.co/bAVsP3sV47
Attracts SharePoint IT Professionals! I do this in SharePoint Content Management
“\List\Some thing\File.jpg”.Split(‘\’).Where({$_},’Last’,1)
Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl https://t.co/YXxJv3Pcl1
RT @juneb_get_help: Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl ht…
RT @juneb_get_help: Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl ht…
How Out-Null can be identical to $null? It is a command, i.e. a user may define a function Out-Null, so that piping to it works differently. This may have useful practical applications, unless “| Out-Null” *syntax* is treated as “> $null”.
RT @juneb_get_help: Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl ht…
RT @juneb_get_help: Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl ht…
RT @juneb_get_help: Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl ht…
RT @juneb_get_help: Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl ht…
@pcgeek86 This from @juneb_get_help seems to contradict your tweet on how the Where method could be used: https://t.co/L0Xn7tmLLH
@texmandie @pcgeek86 @BrucePayette yep! Then @juneb_get_help wrote it all up 🙂 https://t.co/yqU0cMBXte
RT @juneb_get_help: Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl ht…
RT @juneb_get_help: Undocumented feature of #PowerShell 5: Call a method w/o parens. Why is Out-Null slow? @SAPIENTech @BrucePayette @cl ht…
Calling a method without parentheses in #PowerShell version 4+: https://t.co/yiDFpfmbpd <<– @BrucePayette’s answer to controversial topic
@pcgeek86 @psCookieMonster @Jaykul @bielawb @zippy1981 @mikefrobbins Read @juneb_get_help post on this: https://t.co/NV1v9ajFmv
RT @mikefrobbins: Calling a method without parentheses in #PowerShell version 4+: https://t.co/yiDFpfmbpd <<– @BrucePayette’s answer to co…
I understand, Roman. There’s a lot of interpretation code in the PowerShell parser. In this case, although the explicit syntax is different and follows different models, Bruce explained that the underlying parser code for piping to Out-Null and redirecting to $null are the same. For details, tweet a question to Bruce (@BrucePayette) or Jason Shirk (@lzybkr).
RT @mikefrobbins: Calling a method without parentheses in #PowerShell version 4+: https://t.co/yiDFpfmbpd <<– @BrucePayette’s answer to co…
Here is the fact. If in PowerShell v5 we try
function Out-Null { $input }; $(1..1000) | Out-Null
then we get the output. This shows that `| Out-Null` is not the same as `> $null`.
function Out-Null { }
$(1..1000) | Out-Null
Thanks, Roman. You’re absolutely correct that a command and an expression are not the same. I think that Bruce’s point was that the underlying parser code — the code that runs when you pipe $x to Out-Null and redirect $x to $null — are the same. He tech-reviewed and approved the post before I published it, so if you have any questions for him, you can reach him on Twitter at @BrucePayette.