new-blogentry -topic "Powershell and More"

My observations about Powershell, Windows, System Center and life.

Recent comments

Tags

Don't show

    Disclaimer

    Any opinions expressed herein are completely accidental. But if one happens to slip in, it represents my own personal opinion and NO one elses. I'm also not concerned with changing anyone elses opinion, so any rants about anything presented on this site are likely to be 100% ignored.

    © Copyright 2010

    Querying date based Active Directory fields

    * This is a repost from my original blog location *

    Another “from usenet to blog” entry.  Someone wanted to a list of all users in their Active Directory created after a specific day. Like most AD related tasks, this is fairly easy in Powershell, but you do have to be aware of one tricky piece. LDAP queries require a specially formatted string to represent date/time. For this, we’ll query the ‘whenCreated’ field of the AD. Here’s an example that returns all users from AD that were created in the last 15 days:

    $past = [datetime]::UtcNow.adddays(-15)

    $ldappast = "{0:0000}{1:00}{2:00}000000.0Z" -f $past.year,$past.month,$past.day

    $s = new-object directoryservices.directorysearcher([ADSI]'')

    $s.filter = "(&(objectcategory=person)(objectclass=user)(whenCreated>=$ldappast))"

    $s.findall()

    $ldappast holds the date in the specific format needed when comparing date/times in LDAP: YYYYMMDDHHMMSS.TZ. An example of March 12, 2007 00:00 represented in this format would be: 20070312000000.0Z. By the way, “0Z” indicates UTC.

     

    gaurhoth


    Posted by gaurhoth on Monday, March 19, 2007 8:19 AM
    E-mail | Permalink | Comments (17) | Post RSSRSS comment feed