Powershell - Brackets

Powershell支持三种类型的括号.

  • 括号括号:()

  • 大括号括号: {}

  • 方括号:[]

括号括号

此类括号用于

  • 传递参数

  • 附上多套指令

  • 解决歧义

  • 创建数组

示例

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

括号括号

此类括号用于

  • 附上陈述

  • 阻止命令

示例

$x = 10

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

这将产生以下结果 :

输出

 
这是if语句.

方括号

此类括号用于

  • 访问数组

  • 访问哈希表

  • 使用正则表达式过滤

示例

> $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
   ...