Converting a string to a date.

Hi there,

I am looking at automating a resignation process when an a person at an organisation leaves.

Currently the way the service desk do it manually is that when the intranet disables the user it puts the date when the user got disabled in the description property. The way it is written in the Description field is like

"Disabled 29/06 -Refer 1233236 "
and the modified field in AD is written as 29/06/2015

(sorry about the date notation, I am a New Zealander).

They match that with the modified field and after two weeks they then proceed to do the necessary removing of group memberships etc.

The aspect that I am struggling with is the logic Powershell will use to determine based on the requirements of the business, and how that would be written.

I can get the list of users like so

get-aduser -filter * -Properties description | where {$_.description -like "*disabled*"}

but I am unsure how to evaluate it further… to convert to date.

This is an interesting exercise and I would love to have a basis to work off…any ideas/examples that I can use would be most appreciated.

Wei-Yen Tan

Hey Wei-Yen

Try this please.

$desc=“Disabled 29/06 -Refer 1233236”
$a=($desc -split " ")


You need to be careful Yuan because the example date 29/06 parses to 29/06/2016 - if no year is provided it assumes the current year.

Personally I prefer to use a cmdlet over .NET:

$date = Get-Date "29/06"

But this still resolves to 2016, and from what I gather the date is only entered when the user has been disabled, so you will probably need to do something like the following:

Get-ADUser -Filter * -Properties Description |
Where {$_.Description -like "*disabled*"} |
Foreach-Object {
    $split = $_.Description -split " "
    $date = Get-Date $split[1]
    if ($date -gt Get-Date) { $date = $date.AddYear(-1) }
    # Do whatever from here

So if the date is in the future, it knocks a year off.

(By the way don’t apologise for your VERY SENSIBLE date format lol, can’t stand it when games and software forces it into American format with no indication)

Thanks for that deadly-bagel and Yuan.It gave me something to work with and I have played around with it.

Deadly-bagel, I did notice that anomaly happening with the dates. I have highlighted that with the Service Desk so we could have that remedied.

I employed something similar to what you proposed prior although I think using a cmdlet will be easier.

I was outputting the $date variable just to see how it was evaluating it.

Its outputting exactly what I want. If I can confirm that they will accept full dates then all will be well and good or I will be certainly be evaluating your method for sure.

$desc = get-aduser -filter * -properties description, modified | where { $_.description -like "*Disabled*" }
foreach ($d in $desc)
	$a = ($d.description -split "-")
	$datestring = $a[0]
	$date = [datetime]::Parse($datestring)
	$aduser = $d.SamAccountName
	$moddate = Get-Date ($d.modified)
	if ($datestring -gt $moddate.AddDays(-14))

Thank you very much to the both of you for your help. This has been a lot of fun.