Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any other uses of parenthesis in powershell?

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:\)
like image 955
Emiliano Poggi Avatar asked Apr 05 '11 21:04

Emiliano Poggi


People also ask

What do parentheses do in PowerShell?

You can use enclosures, such as parentheses, to override the standard precedence order and force PowerShell to evaluate the enclosed part of an expression before an unenclosed part.

What is the significance of () curly brackets in PowerShell?

The curly braces direct PowerShell to interpret the variable name's characters as literals.

Why we use $_ in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


2 Answers

Cause a statement to yield a result in an expression:

($x=3) + 5   # yields 8
like image 157
Joey Avatar answered Sep 21 '22 13:09

Joey


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.

like image 31
stej Avatar answered Sep 24 '22 13:09

stej