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??

Cataster
  • 3,081
  • 5
  • 32
  • 79
  • 1
    I just copy/pasted your code including the first line, and it outputs `value1,value2` – trebleCode Feb 12 '19 at 20:15
  • @trebleCode can you try setting $variable to NULL and try that? – Cataster Feb 12 '19 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 – trebleCode Feb 12 '19 at 20:19
  • Possible duplicate of [Check if a string is not NULL or EMPTY](https://stackoverflow.com/questions/45008016/check-if-a-string-is-not-null-or-empty) –  Feb 12 '19 at 20:26

2 Answers2

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.

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
0

\S is nonwhite space.

\s is whitespace.

George
  • 6,886
  • 3
  • 44
  • 56
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27