You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中-ilike运算符匹配字符串未返回True问题咨询

PowerShell -ilike 返回False的原因及解决方法

你的代码:

PS C:\Users\Administrator> $string = "hello world"
PS C:\Users\Administrator> $string -ilike "hello"
False

问题原因

-ilike 是PowerShell的通配符匹配运算符,默认要求模式与目标字符串完全匹配。你的目标字符串是"hello world",而匹配模式"hello"没有通配符,两者不完全一致,因此返回False。

解决办法

有几种方式可以实现“检查字符串是否包含指定子串”的需求:

  • 用通配符配合-ilike:在模式前后加上*(匹配任意长度的任意字符),不区分大小写:

    $string -ilike "*hello*"
    
  • 使用-match运算符:默认不区分大小写,直接匹配子串:

    $string -match "hello"
    
  • 字符串Contains()方法:注意此方法默认区分大小写,如需不区分,可先统一转换大小写:

    # 区分大小写
    $string.Contains("hello")
    # 不区分大小写
    $string.ToLower().Contains("hello".ToLower())
    

内容的提问来源于stack exchange,提问作者ctappy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:54:51