Powershell-brackets

提供:Dev Guides
移動先:案内検索

Powershell-ブラケット

Powershellは3種類のブラケットをサポートしています。

  • 括弧括弧。 −()
  • *中かっこ。 − \ {} *
  • *角括弧。 − [] *

かっこ

このタイプのブラケットは

  • 引数を渡す
  • 複数の命令セットを同封する
  • あいまいさを解決する
  • 配列を作成する

> $array = @("item1", "item2", "item3")

> foreach ($element in $array) { $element }
item1
item2
item3

中かっこ

このタイプのブラケットは

  • 文を囲む
  • ブロックコマンド

$x = 10

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

これは、次の結果を生成します-

出力

This is if statement.

角括弧

このタイプのブラケットは

  • 配列へのアクセス
  • ハッシュテーブルへのアクセス
  • 正規表現を使用したフィルター

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