LoginSignup
2
4

【Powershell】よく使う文字列、数値比較

Last updated at Posted at 2020-10-19

演算子

演算子 説明 大文字小文字区別
-eq 左辺 = 右辺 なし
-ne 左辺 ≠ 右辺 なし
-ceq 左辺 = 右辺 あり
-cne 左辺 ≠ 右辺 あり
-ge 左辺 ≧ 右辺
-gt 左辺 > 右辺
-le 左辺 ≦ 右辺
-lt 左辺 < 右辺
-like ワイルドカードによる比較 なし
-clike ワイルドカードによる比較 あり
-notlike ワイルドカードによる比較 なし
-contains 配列に含まれる なし
-notcontains 配列に含まれない なし
-match 正規表現による比較 なし
-notmatch 正規表現による比較 なし

Stringクラスメソッド

メソッド 説明 大文字小文字区別
IsNullOrEmpty 検査対象の文字列がNullまたはEmptyであればTrueを返す [string]::IsNullOrEmpty("検査対象文字列")
Contains 比較文字列が含まれていればTrueを返す あり "検査対象文字列".Contains("比較文字列")

使い方

数値比較

#-------------------------------
# =:等しい
#-------------------------------
PS C:\> 10 -eq 10
True

#-------------------------------
# ≠:等しくない
#-------------------------------
PS C:\> 10 -ne 9
True

#-------------------------------
# ≧:大なりイコール
#-------------------------------
PS C:\> 10 -ge 10
True

PS C:\> 10 -ge 9
True

#-------------------------------
# >:大なり
#-------------------------------
PS C:\> 10 -gt 10
False

PS C:\> 10 -gt 9
True

#-------------------------------
# ≦:小なりイコール
#-------------------------------
PS C:\> 10 -le 11
True

PS C:\> 10 -le 10
True

#-------------------------------
# <:小なり
#-------------------------------
PS C:\> 10 -lt 11
True

PS C:\> 10 -lt 10
False

文字列比較

#-------------------------------------------
# [string]::IsNullOrEmpty:NullまたはEmpty
#-------------------------------------------
PS C:\> [string]::IsNullOrEmpty("")
True

#-------------------------------------------
# -eq:等しいか(大文字小文字区別なし)
#-------------------------------------------
PS C:\> "AB" -eq "ab"
True

#-------------------------------------------
# -ne:等しくないか(大文字小文字区別なし)
#-------------------------------------------
PS C:\> "AB" -ne "ab"
False

#-------------------------------------------
# -ceq:等しいか(大文字小文字区別あり)
#-------------------------------------------
PS C:\> "AB" -ceq "ab"
False

PS C:\> "AB" -ceq "AB"
True

#-------------------------------------------
# -ceq:等しくないか(大文字小文字区別あり)
#-------------------------------------------
PS C:\> "AB" -cne "ab"
True

PS C:\> "AB" -cne "AB"
False

#-------------------------------------------
# -like:ワイルドカード比較(大文字小文字区別なし)
#-------------------------------------------
PS C:\> "aaa" -like "A*a"
True

PS C:\> "aaa" -like "A*b"
False


#-------------------------------------------
# -notlike:ワイルドカード比較(大文字小文字区別なし)
#-------------------------------------------
PS C:\> "aaa" -notlike "a*a"
False

PS C:\> "aaa" -notlike "a*b"
True

#-------------------------------------------
# -clike:ワイルドカード比較(大文字小文字区別あり)
#-------------------------------------------
PS C:\> "aaa" -clike "a*a"
True

PS C:\> "aaa" -clike "A*a"
False


#-------------------------------------------
# -contains:含む(大文字小文字区別なし)
#-------------------------------------------
PS C:\> "ABC","def","ghij" -contains "ABC"
True

PS C:\> "ABC","def","ghij" -contains "abc"
True

PS C:\> "ABC","def","ghij" -contains "A"
False

#-------------------------------------------
# -notcontains:含まない(大文字小文字区別なし)
#-------------------------------------------
PS C:\> "ABC","def","ghij" -notcontains "ABC"
False

PS C:\> "ABC","def","ghij" -notcontains "abc"
False

PS C:\> "ABC","def","ghij" -notcontains "A"
True

PS C:\> "ABC","def","ghij" -notcontains "a"
True

#-------------------------------------------
# .contains:含む(大文字小文字区別あり)
#-------------------------------------------
PS C:\> "ABC".Contains("A")
True

PS C:\> "ABC".Contains("a")
False

#-------------------------------------------
# -match:正規表現でマッチしている(大文字小文字区別なし)
#-------------------------------------------
# . :任意の1文字
# *:直前の文字が0回以上
# +:直前の文字が1回以上

PS C:\> "GoldEgg" -match "go*..eg+"
True

PS C:\> $Matches

Name                           Value                                                                                                                                                                                                                                                                                   
----                           -----                                                                                                                                                                                                                                                                                   
0                              GoldEgg      

#-------------------------------------------
# -notmatch:正規表現でマッチしていない(大文字小文字区別なし)
#-------------------------------------------
PS C:\> "GoldEgg" -match "Go+.Eg*"
False

PS C:\> "GoldEgg" -notmatch "Go+.Eg*"
True

PS C:\> $Matches

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4