I’m trying to follow this WPF tutorial. The guy uses syntax like this:
$Button.Add_Click{$CurrentValue ++}
Powershell 2.0 is unhappy about it but in 5.0 it’s fine. Well I know that Add_Click is a method so I replaced it with
$Button.Add_Click({$CurrentValue ++})
and 2.0 swallowed it.
But the question is since what version the omission of parentheses in method calls became legitimate?
I’ve always known it to be () in all versions. You might ask Jvierra over on the sapien forums, he’s been around a lot longer than I.
The {} is a script block
PS C:\Users\Rob> $test = {}
PS C:\Users\Rob> $test.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ScriptBlock System.Object
As Powershell has progressed, the developers attempt to take redundant code and let you use ‘shortcuts’. For instance, in v2 you have to do | Where {$_.Param -eq “Blue”} and in v3 and above you can do Where Param -eq “Blue”. The language is built for administrators and simplicity. If you are writing a production script and not a quick query, it’s best to make the code as strongly-typed as possible, in IMHO. I use my code in multiple environments and many, sadly, still use V2, which makes me ensure that all code runs on V2 - V5.