Share via

how to check a value is that null in powershell script

Peizhi_Yu 6 Reputation points
Sep 11, 2020, 5:00 AM

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,628 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue-MSFT 41,606 Reputation points Microsoft External Staff
    Sep 11, 2020, 6:58 AM

    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.