question

adityadugyala-0395 avatar image
0 Votes"
adityadugyala-0395 asked RichMatheisen-8856 answered

Powershell command - null or empty comand is not working

Tried both the cases to check if it is nullorempty but eventhough its null the statement is passing
$RequestorEmail=""

case1
if(($RequestorEmail -ne $null) -or ($RequestorEmail -ne ""))

case2
if ( -not [string]::IsNullOrEmpty( $RequestorEmail ) )

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @adityadugyala-0395 ,

maybe this helps:

 $a = ""
 if ($a) { 
     Write-Output "variable contains value" 
 }
 else { 
     Write-Output "variable is empty"
 }

Or vice versa:

 $a = ""
 if (!$a) {    
     Write-Output "variable is empty"
 }
 else { 
     Write-Output "variable contains value"
 }


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

In the situation you present, $RequestorEmail contains an empty string.

In your 1st test, since the variable $RequestorEmail is NOT $null (because it holds an empty string), and the string DOES contain an empty string, both conditions fail so the result is FALSE.

In your 2nd test, the variable $RequestorEmail IS empty and the IsNullOrEmpty is true, but you're negating that with the -not operator, so the result is FALSE.

What results where you expecting? And why?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.