I've created a new AD Field that I want to save the date of birth of users in our AD...

We do the creation via Powershell script but I'm having problems saving this field because of the date format... I think AD saves like yyyymmddhhmmss format but I can't figure it out how to convert to this value...

Anyone has ideas? My current code is:

$MyDateVar= '19830613000000' 
Set-ADUser $samaccountname -replace @{MyField=datetime]::fromFileTime($MyDateVar)}

It saves the date but likee 23/01/01 22:51:01 instead of 1983/06/13 00:00:00

  • 1
    I'd also suggest, that you don't create a "new AD Field", but use the provided extensionAttribute[1-15]. That's what they're there for so you don't have to meddle with new attributes. – Lenniey Feb 1 '18 at 12:11
  • 1
    If you're not running Exchange, you don't necessarily have those attributes in a bare bones AD. Besides, a strongly typed and well named attribute is arguably a better solution so long as you don't do dumb things with OIDs. – Ryan Bolger Feb 1 '18 at 17:29
  • @RyanBolger Ah well yes, you're right with Exchange. I almost never work on ADs without some form of Exchange, so I forgot. Then you should have to create your own. – Lenniey Feb 2 '18 at 7:52

A few issues:

First up, you're missing a square bracket from your example datetime]. I'm guessing this is just a transcription error.

You appear to be trying to use FileTime but with a concatenated string. FileTime refers to a specific type of value, that's "a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601".

As it happens, the result you mention would have been saving the date as 23/01/1601 (or, literally, 01/01/1601 + (19830613000000 * 100ns)).

FileTime is what Active Directory is expecting in date field, so you actually want to convert to FileTime, not from.

Depending on how you're sourcing the data, you could convert it before import, or during the call - here's a rubbish example of splitting your date-string:

$MyDateVar= '19830613000000'
$Birthday = @{
    Year   = -join$MyDateVar[0..3]
    Month  = -join$MyDateVar[4..5]
    Day    = -join$MyDateVar[6..7]
    Hour   = 0
    Minute = 0
    Second = 0
$BirthdayFileTime = (Get-Date @Birthday).ToFileTime()

Set-ADUser $SAMAccountName -Replace @{Birthday=$BirthdayFileTime}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.