Powershell: how to check if string contains any significant characters?

25,236

Use the -match operator:

if ($test -match 'regex_here') { 'It matched' }

Also check the online docs for comparison operators: http://technet.microsoft.com/en-us/library/hh847759.aspx

Share:
25,236
Jeff
Author by

Jeff

Updated on May 07, 2020

Comments

  • Jeff
    Jeff about 4 years

    I need to check a string to see if it conatins anything other than spaces, returns, etc.

    In perl, I used:

    if($val =~/^\s*$/) {...}
    

    How do I do that in PowerShell?

  • Jeff
    Jeff about 11 years
    I found ss64.com/ps/syntax-regex.html to be helpful. It looks like what I want is, $string -match "\s+"
  • Andy Arismendi
    Andy Arismendi about 11 years
    Jeff, another trick is $val.Trim().length. That will delete all leading/trailing whitespace characters. length will be 0 if all that's all there was.