powershell convert whenchanged to get-date RRS feed

  • Question

  • Hi,

    I try to write a powershell script to manage my AD account in special OU.

    As you know when we want to change something on our accounts in AD "whenchanged" show us which date something changed in this account.

    I want to use powershell to get "whenchanged" date of this account and doing (plus/minus) with Get-Date output which. But the problem is whenchanged output is an object but get-date output is string or int, and I cant do minus/plus those two output.

    Get-ADUser -filter {Enabled -eq $True} -SearchBase 'OU=testOU,DC=Home,DC=cloud' -properties whenChanged | Sort-Object DisplayName | Select-Object DisplayName, whenChanged

    DisplayName whenChanged        
    ----------- -----------        
                2019-09-06 15:21:48
                2019-09-06 15:24:02

    PS C:\Windows\system32> (Get-Date)

    den 10 september 2019 13:41:47

    I have two account in this OU, I want to do something like this....... IF (whenchanged - Get-date) ge 180 

    here 180 means 6 monthe....If the result is more than 180 days or 6 months print "Hello".

    how can i use powershell to do like this????

    Thank you

    Tuesday, September 10, 2019 11:49 AM

All replies

  • Hi,

    you are able to convert a string to the datetime object 

    [datetime]"2019-09-06 15:21:48"

    the output is (using a german powerrshell.

    Freitag, 6. September 2019 15:21:48 

    kind regards


    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com

    Tuesday, September 10, 2019 1:34 PM
  • The whenChanged attribute has format GeneralizedTime, which is a string in the form YYYYMMDDhhmmss.0Z. You can use the .NET [datetime] to convert into an actual datetime value. The Get-Date cmdlet only deals with datetime values, not strings. But the Get-ADUser cmdlet converts whenChanged into a datetime. A common way to query with this attribute would be similar to below:

    $Date = (Get-date).adddays(-60)
    $Users = Get-ADUser -Filter{Whenchanged -ge $Date}

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Tuesday, September 10, 2019 5:45 PM
    Tuesday, September 10, 2019 1:55 PM
  • Thank you for the respond
    Friday, September 13, 2019 7:33 AM
  • thank you for respond....

    I solved it with this form :

    $userinfo = Get-ADUser -filter {Enabled -eq $True} -SearchBase 'OU=testOU,DC=DR,DC=cloud' -properties whenChanged | Sort-Object DisplayName | Select-Object DisplayName, whenChanged 
         $today = Get-Date -Format "yyyy-MM-dd"
         $userData =$userinfo.Whenchanged
         $convert = $userData.ToshortDatestring()

         $result = (New-TimeSpan -Start $convert -End $today).Days 

    Friday, September 13, 2019 7:37 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Friday, October 4, 2019 8:22 AM