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
Author by
Jeff
Updated on May 07, 2020Comments
-
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 about 11 yearsI found ss64.com/ps/syntax-regex.html to be helpful. It looks like what I want is, $string -match "\s+"
-
Andy Arismendi about 11 yearsJeff, 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.