Sune
Sune

Reputation: 3270

Check to see if a variable is empty (multiple checks!)

I am trying to run something if A is the same as B and B is not empty. I can do this check manually before I do thi -NE, but I rather do this in the same check.

if($($ADEmployee.mobile) -ne $($ListEmployee.mobile) -and (!$($ListEmployee.mobile)))
{
  do someething
}

can this be done?

Upvotes: 1

Views: 13688

Answers (3)

Sitaram Pamarthi
Sitaram Pamarthi

Reputation: 459

I feel using IsNullOrEmpty method of system.string object is more appropriate for checking null or empty values. It also has method to check if a string is formed by only white spaces which is useful. See http://techibee.com/powershell/check-if-a-string-is-null-or-empty-using-powershell/1889 for more details.

Upvotes: 1

zx38
zx38

Reputation: 114

Yes, I would check if "empty" first then check the other object for "not empty". Subexpressions are not necessary:

if (!$ListEmployee.mobile -and $ADEmployee.mobile)
{
    do someething
}

Update: $( ... ) is a subexpression. Try the following then:

if ($ListEmployee.mobile -and $ListEmployee.mobile -ne $ADEmployee.mobile) 
{
    $ADEmployee.mobile = $ListEmployee.mobile
}

Upvotes: 1

mjolinor
mjolinor

Reputation: 68273

 if (($ademployee.mobile,$null -like $listemployee.mobile)[0]){
      do something
      }

Not that I'd ever actually do that in code.

Upvotes: 1

Related Questions