PowerShell 条件分岐 if -eq 以外の比較演算子

Pocket

PowerShell 7 で使えそうな 比較演算子一覧を下記です。

バージョンが上がれば それ以外の 演算子が増えるかもしれません

大小文字区別なし 大小文字区別なし 大小文字区別
-eq -ieq -ceq ==
-ne -ine -cne !=
-gt -igt -cgt >
-ge -ige -cge >=
-lt -ilt -clt <
-le -ile -cle <=
-like -ilike -clike

$a = “日本”

if($a -like “日*”){$a}

-notlike -inotlike -cnotlike マッチしない
-match -imatch -cmatch 正規表現

$match1 = ‘^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$’

$ipaddr = “127.0.0.1”

if ($ipaddr -match $match1){

$true}

-notmatch -inotmatch -cnotmatch 正規表現が一致しない場合
-replace -ireplace -creplace

($match1 -replace $ipaddr)↑-matchの逆

-contains -icontains -ccontains 配列などに存在するか

$a = (1,2,3)

if ($a -contains 2){}

-notcontains -inotcontains -cnotcontains 配列などに無い場合
-in    

$a = 1

if ($a -in (1,2,3)){$true}

-notin     inに無い場合
-is     型が一致しているか
if ($a -is [string]){}
-isnot     型が一致しない場合

-replace のコード例

$match1 = '^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$'
$ipaddr = "127.0.0.1"
if ($match1 -replace $ipaddr){
    Write-Host "IPアドレス"
}

learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_comparison_operators

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED