7

As new to Powershell world, sometime I'm stuck in the tricky syntax. That's why I'm trying to figure out all the possible uses of the parenthesis inside the language.

Do you know some more? Can you add here?

Here mine (left out basic use of curly in pipeline and round in method calls):

# empty array
$myarray = @()

# empty hash
$myhash = @{}

# empty script block
$myscript = {}

# variables with special characters
${very strange variable @ stack !! overflow ??}="just an example"

# Single statement expressions
(ls -filter $home\bin\*.ps1).length

# Multi-statement expressions inside strings
"Processes: $($p = “a*”; get-process $p )"

# Multi statement array expression
@( ls c:\; ls d:\)
JYelton
  • 35,664
  • 27
  • 132
  • 191
Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
  • Only parentheses (`()`) or (following your examples) braces (`{}`) too? What about brackets (`[]`)? – Joey Apr 05 '11 at 21:24
  • Yes, braces and brackets also...sorry for my poor english. Anyway braces are not curly brackets (or parenthesis as well)? :) – Emiliano Poggi Apr 05 '11 at 21:27

6 Answers6

6

Cause a statement to yield a result in an expression:

($x=3) + 5   # yields 8
Joey
  • 344,408
  • 85
  • 689
  • 683
  • Wow. Don't think I'll never use it, it's tricky anyway! Not able to catch the first statement really. Shouldn't be `($x=3) + 5`? – Emiliano Poggi Apr 05 '11 at 21:33
  • Indeed, sorry. There was a `$` missing. That's what you get for writing C# momentarily :-) – Joey Apr 05 '11 at 21:51
  • That is odd behavior, but it explains some weirdness I was seeing today. BTW, your second usage is just variables with special characters. – JasonMArcher Apr 05 '11 at 23:12
  • @jasonmarcher - actually, his second usage is not really about variables at all. It's an indirection equivalent to get-content, but this is exactly how it works for the variable drive also. $foo is equivalent to ${variable:foo}. Think of "variable:" as the default provider in powershell. – x0n Apr 06 '11 at 03:10
4

When using generics, you need to wrap the type in [..]:

New-Object Collections.Generic.LinkedList[string]

For some people this might look confusing, because it is similar to indexing in arrays.

stej
  • 28,745
  • 11
  • 71
  • 104
2

The Param( ) statement (in a function, script, or scriptblock)

Around the condition in an If (or Elseif statement)

Around the expression in a switch statement.

Edit: Forgot the condition in the while statement.

Edit2: Also, $() for subexpressions (e.g. in strings).

Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
  • About Param, if, switch and while, you are true but they were left out appositely (the same is for **foreach** and **for**). The last you proposed is already in my list. Thanks for your answer anyway. – Emiliano Poggi Apr 05 '11 at 21:45
  • Subexpressions are not in the list. – stej Apr 05 '11 at 22:29
  • Might be I'm missing something...are you not referring to this: `"Processes: $($p = “a*”; get-process $p )`, this a subexpression indeed and is in my list. – Emiliano Poggi Apr 06 '11 at 06:43
1

The parenthesis is most powerfully.

Suppose that you want collect all output, including errors, of some scriptblock and redirect to a variable or another functions for handle this... With the parenthesis, this is easy task:

$customScript = {  "This i as test"; This will be procedure error!  }

(. $customScript 2>&1 ) | %{"CAPTURING SCRIPT OUTPUT: "+$_}
benka
  • 4,732
  • 35
  • 47
  • 58
Rodrigo
  • 92
  • 6
1

Regular expressions are arguably a first-class construct in Powershell.

If we're compiling a complete list, we can include the role that square and round brackets play in regular expressions.

An example:

$obj.connectionString = $obj.connectionString -replace '(Data Source)=[^;]+', '$1=serverB\SQL2008_R2'

Because of the support for XML, you can go so far as to include the square brackets used in XPath. (That's really drawing a long bow though :-)

select-xml $config -xpath "./configuration/connectionStrings/add[@name='LocalSqlServer']"
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
  • Since regular expressions are always given in strings they are not part of the PowerShell syntax, though. That also makes them definitely not first-class constructs :-) – Joey Apr 06 '11 at 06:23
  • Yes, I think so. Regular expressions do not fit here. Thanks anyway. – Emiliano Poggi Apr 06 '11 at 06:48
  • I wasn't sure, so making the contentious statement was a good way to test the definition. :-) There's more on the definition of "first-class" here: http://stackoverflow.com/questions/646794/what-is-a-first-class-programming-construct – Andrew Shepherd Apr 06 '11 at 10:11
0

It's even written, but not enough clearly in the first short list after "Multi-statement expressions inside strings I'will add

# Var property inside a string
$a = get-process a*
write-host "Number of process : $a.length" # Get a list of process and then ".length
Number of process : System.Diagnostics.Process (accelerometerST) System.Diagnostics.Process (AEADISRV) System.Diagnostics.Process (agr64svc).length

write-host "Number of process : $($a.length)" # To get correct number of process
Number of process : 3
JPBlanc
  • 70,406
  • 17
  • 130
  • 175