「PowerShell」switch文にExactを使用して大文字と小文字を区別しない

構文
$変数名 =値

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
switch -Exact ($変数名){
  条件 or 値{
    処理コード
    break}
・・・
  default{
   処理コード}
}
switch -Exact ($変数名){   条件 or 値{     処理コード     break} ・・・   default{    処理コード} }
switch -Exact ($変数名){
  条件 or 値{
    処理コード
    break}
・・・
  default{
   処理コード}
}

式の結果と値が一致すれば、配下の処理が実行されます。
breakでswitch文を抜けます。
Exactは、大文字小文字を区別しません。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$tt = "toto"
switch -Exact ($tt)
{
"toto" {Write-Output ("AA 11"); break}
"Toto" {Write-Output ("BB 22"); break}
"TOTO" {Write-Output ("CC 33"); break}
default {Write-Output ("other data");}
}
$tt = "toto" switch -Exact ($tt) { "toto" {Write-Output ("AA 11"); break} "Toto" {Write-Output ("BB 22"); break} "TOTO" {Write-Output ("CC 33"); break} default {Write-Output ("other data");} }
$tt = "toto"

switch -Exact ($tt)
{
    "toto" {Write-Output ("AA 11"); break}
    "Toto" {Write-Output ("BB 22"); break}
    "TOTO" {Write-Output ("CC 33"); break} 
    default {Write-Output ("other data");}
}

実行結果
PS C:\study\powershell> ./test.ps1
AA 11

PowerShell

Posted by arkgame