Powershell - Brackets


Advertisements

Powershell supports three types of brackets.

  • Parenthesis brackets. − ()

  • Braces brackets. − {}

  • Square brackets. − []

Parenthesis brackets

This type of brackets is used to

  • pass arguments

  • enclose multiple set of instructions

  • resolve ambiguity

  • create array

Example

> $array = @("item1", "item2", "item3")
 
> foreach ($element in $array) { $element }
item1
item2
item3

Braces brackets

This type of brackets is used to

  • enclose statements

  • block commands

Example

$x = 10

if($x -le 20){
   write-host("This is if statement")
}

This will produce the following result −

Output

This is if statement.

Square brackets

This type of brackets is used to

  • access to array

  • access to hashtables

  • filter using regular expression

Example

> $array = @("item1", "item2", "item3")
 
> for($i = 0; $i -lt $array.length; $i++){ $array[$i] }
item1
item2
item3
 
>Get-Process [r-s]*
 Handles    NPM(K)     PM(K)    WS(K)   VM(M)   CPU(s)     Id    ProcessName
-------    ------     -----     -----   -----   ------     --    -----------  
    320        72     27300     33764    227     3.95    4028    SCNotification 
   2298        77     57792     48712    308             2884    SearchIndexer
   ...
Advertisements