вам нужны строковые методы ИЛИ регулярное выражение и оператор -match
.[ ухмылка ]
вот один способ увидеть методы ...
'asdf' |
Get-Member -MemberType Methods
вот один способ увидеть статические методы типа [string]
..
'asdf' |
Get-Member -Static
для регулярных выражений, я рекомендую regex101.com ... [ ухмылка ]
вот несколько демонстрационных кодов для задействованных идей ...
$StringList = @(
'qwerty is at the start of this string'
'at the string-end, is qwerty'
'in the middle, qwerty is placed'
'the target word is not in this string'
# below is an empty string
''
)
$Target = 'qwerty'
foreach ($SL_Item in $StringList)
{
'+' * 50
'--- Current test string = "{0}"' -f $SL_Item
'=== String Methods ==='
'Target at start = {0}' -f $SL_Item.StartsWith($Target)
'Target at end = {0}' -f $SL_Item.EndsWith($Target)
'Target anywhere in string = {0}' -f $SL_Item.Contains($Target)
''
'=== Regex ==='
'Target at start = {0}' -f ($SL_Item -match "^$Target")
'Target at end = {0}' -f ($SL_Item -match "$Target$")
'Target anywhere in string = {0}' -f ($SL_Item -match $Target)
''
'=== Empty ==='
'String is $Null or empty = {0}' -f [string]::IsNullOrEmpty($SL_Item)
'String is $Null or empty = {0}' -f ([string]::Empty -eq $SL_Item)
''
}
усеченный вывод ...
++++++++++++++++++++++++++++++++++++++++++++++++++
--- Current test string = "qwerty is at the start of this string"
=== String Methods ===
Target at start = True
Target at end = False
Target anywhere in string = True
=== Regex ===
Target at start = True
Target at end = False
Target anywhere in string = True
=== Empty ===
String is $Null or empty = False
String is $Null or empty = False
[*...snip...*]
++++++++++++++++++++++++++++++++++++++++++++++++++
--- Current test string = ""
=== String Methods ===
Target at start = False
Target at end = False
Target anywhere in string = False
=== Regex ===
Target at start = False
Target at end = False
Target anywhere in string = False
=== Empty ===
String is $Null or empty = True
String is $Null or empty = True