if文
if () {
}
elseif () {
}
else {
}
for文
for ($i = 0; $i -lt 5; $i++) {
}
while文
while ( $i -lt 5 ) {
$i++
}
複数の変数をまとめて初期化
$a = $b = $c = 1
関数の使い方
# パラメータ無し
Function FuncName
{
}
# 未定義のパラメータ
## 関数
Function FuncName
{
${args}
}
## 呼び出し方
FuncName param1 param2
# 定義済みのパラメータ
## 関数
Function FuncName($param_a, $param_b)
{
$param_a
$param_b
}
## 呼び出し方
FuncName -param_a param1 -param_b param2
Pathの追加
Write-Output "org"
$Env:Path
# Lhaplusのpathを追加する
$Env:Path+=";C:\Program Files\Lhaplus"
Write-Output "new"
$Env:Path
スイッチパラメータ
## 関数
Fubction Get-Switch ( [switch] $l_switch, $data = "ON" )
{
process {
if ($l_switch) {
"switch is ${data}"
else {
"switch is OFF"
}
}
## 呼び出し方(スイッチを指定)
Get-switch -l_switch
### 出力
switch is ON
## 呼び出し方(スイッチを指定しない)
Get-switch
### 出力
switch is OFF
ファイルの読み込み
ForEach ($line in Get-Content data.txt) {
$line
}
引数チェック
## 関数
Function ArgCheck($x = $(Throw '引数が不正'))
{ "x = $x" }
最終更新:2009年05月06日 10:53