how to check a value is that null in powershell script

Peizhi_Yu 1 Reputation point
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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,361 Reputation points Microsoft Vendor
    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.