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

PowerShell 比較演算子一覧

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

C# dotnet ディレクトリ Path区切り文字¥かスラッシュかを判断

DirectorySeparatorChar

C# .NET 6 ( dotnet )で、 macOSWindows両方対応したコードを書く場合

ディレクトリのPath名が、 「¥」なのか「/」なのかを判断するための

静的フィールドが用意されています。

using System.IO;

char separator = Path.DirectorySeparatorChar;
Console.WriteLine("{0}", separator);

上記↑のコードを macOSで実行すると 結果は「/」です

macOSで実行すると 結果は「/」

Windowsでは「¥」が返ります。

その他のセパレーター

char altDirSeparator = Path.AltDirectorySeparatorChar;
Console.WriteLine("{0}", altDirSeparator);
char pathSeparator = Path.PathSeparator;
Console.WriteLine("{0}", pathSeparator);
char volumeSeparator = Path.VolumeSeparatorChar;
Console.WriteLine("{0}", volumeSeparator);
2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED