0

I have a variable that is holding two values from a table (using -expandproperty)

$variable = "value1,value2"

I have a check to make sure $variable doesnt have any NULL/empty value returned

if(($variable.Split(",") | Where { $_ -match '^\S'}).count -lt 1)
{write "no value!"}
else
{write $variable}

but for some reason, even though '^\S' is supposed to ONLY check the first character/index for a whitespace, it outputs "

no value

" condition result...it should be outputting

value1,value2

instead...

not only that, but if the table column value is NULL, it doesnt recognize it and instead tried to output the second condition write $variable

why is that??

4
  • 1
    I just copy/pasted your code including the first line, and it outputs value1,value2 Commented Feb 12, 2019 at 20:15
  • @trebleCode can you try setting $variable to NULL and try that? Commented Feb 12, 2019 at 20:16
  • When setting $variable to $Null, get a You cannot call a method on a null-valued expression error message. if setting the Where statement to use a lowercase s, it returns 'no value' but returns values with capital S Commented Feb 12, 2019 at 20:19
  • Possible duplicate of Check if a string is not NULL or EMPTY Commented Feb 12, 2019 at 20:26

2 Answers 2

1

another way to check for "null or empty" is to use the [string]IsNulOrEmpty() static method. something like this ...

$Var1 = "value1,value2"
$Var2 = ''
$Var3 = $Null

$Var1, $Var2, $Var3 |
    ForEach-Object {
        $_
        [string]::IsNullOrEmpty($_)
        }

output ...

value1,value2
False

True
True

note that the final $Null did not produce a new line, while the 2nd item [an empty string] did.

Sign up to request clarification or add additional context in comments.

2 Comments

THANK YOU! this is an even better solution!
you are most welcome! glad to help a tad ... [grin]
0

\S is nonwhite space.

\s is whitespace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.