基本構文(14)

今さらという感じもしますが、条件分岐文の書き方です。

if文

$a = 10
if ($a -eq 10)
{
    write-host True
}
else
{
    write-host False
}

switch文

$a = 2
switch($a)
{
    1 {"one"; break}
    2 {"two"; break}
    3 {"three"; break}
}

switch文(正規表現)

ルートディレクトリ以外にいる場合
MSH C:\temp> $a = get-location
MSH C:\temp> switch -regex ($a.path)
>> {
>>     "(?<dir>[^\\]+$)" {write-host Current Directory is $matches.dir}
>>     default {write-host In the root directory}
>> }
>>
Current Directory is temp
MSH C:\temp>
ルートディレクトリにいる場合
MSH C:\> $a = get-location
MSH C:\> switch -regex ($a.path)
>> {
>>     "(?<dir>[^\\]+$)" {write-host Current Directory is $matches.dir}
>>     default {write-host In the root directory}
>> }
>>
In the root directory

?」という正規表現の意味はまたの機会に。