how to check a value is that null in powershell script

Peizhi_Yu 6 Reputation points
2020-09-11T05:00:52.607+00:00

I want to check the PasswordLastSet value of the user is that empty.

PS Z:\> Get-ADUser -Identity test1 -Properties * | select passwordlastset

passwordlastset
---------------

By if to judgment is that null.

this is my script

$password = Get-ADUser -Identity test1 -Properties * | select passwordlastset
 if ($password -ne $null){
    Write-Output "It's empty"
    }
else {
    Write-Output "It isn't empty"
   }

That always output "It's empty"

what should I do? How to change it?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2020-09-11T06:58:05.72+00:00

    Hi,
    The $password is an object of type ADUser. You'd use the property $password.passwordlastset in the if condition and the comparison operator should be -eq. Select-Object returns not the specified property but an object that has only the specified property.

     $password = Get-ADUser -Identity test1 -Properties * | select passwordlastset  
      if ($password.passwordlastset -eq $null){  
         Write-Output "It's empty"  
         }  
     else {  
         Write-Output "It isn't empty"  
        }  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.