none
PowerShell - Check Date in String RRS feed

  • General discussion

  • Hello everyone.  I am trying to create a PowerShell script to check AD for any accounts were disabled at least 30 days ago.

    I have a script will disable the accounts, and set the Telephone note field to a value of "Account Disabled <current date>".

    My script (listed below) should check that note field and any accounts that were disabled at least 30 days before should be listed.  My problem is the way this is written is that it is an exact match to the date 30 days ago, and I want it to search for greater then 30 days.

    $DisableDate = (get-date).AddDays(-30).toString("MM/dd/yyyy")
    get-aduser -Filter * -properties * | Where-Object {$_.info -like "*Account disabled $DisableDate"} | `
    select DisplayName, SAMAccountName, Description, info

    I thought about using split and storing the value in a variable then doing a comparison, but that does not seem to be efficient since it could be several hundred accounts being processed.

    Is there a better option for doing this check?

    Thank you to everyone for your assistance.

    Chris P
    • Changed type Bill_Stewart Wednesday, February 14, 2018 7:22 PM
    • Moved by Bill_Stewart Wednesday, February 14, 2018 7:22 PM User should not be doing this
    Wednesday, January 3, 2018 6:20 PM

All replies

  • You have to convert the date in the note into a date object as strings cannot be compared as dates.


    \_(ツ)_/

    Wednesday, January 3, 2018 6:38 PM
  • Why are you using the info attribute from the user account?

    I recommend using Search-ADAccount.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, January 3, 2018 6:38 PM
  • GetAdUser -Filter * -Properties info, DisplayName, Description |
        Where-Object { 
            if($_.info -match '{\d+\\\d+\\\d+)'){
                $disabledDate = [datetime]$matches[1]
                if($disabledDate -lt [datetime]::Today.AddDays(-30)){
                    $true
                }else{
                    $false
                }
            }
        } |
        Select-Object DisplayName, SAMAccountName, Description, info


    \_(ツ)_/


    • Edited by jrv Wednesday, January 3, 2018 6:44 PM
    Wednesday, January 3, 2018 6:43 PM