Share via


Days of Service and Service Anniversaries

Not an exact science*, but PowerShell and Active Directory can be used to arrive at the length of service for people in your team.

*this post assumes that the WhenCreated date on user accounts corresponds to an employee's actual start date.

 

Take a look at this...

 
$Team | ForEach-Object {

    Get-ADUser -Filter {Name -like $_} -Properties WhenCreated | 
    Select-Object Name,@{n='DaysOfService';e={($((Get-Date)) - $($_.WhenCreated)).Days}},`
                       @{n="ServiceAnniversaryIn$($(Get-Date).Year)";e={$((Get-Date).Year) - $($_.WhenCreated.Year)}}
   
                                         
} | Format-Table -AutoSize

capture187

 

Nothing ground-breaking, I'm just using Get-ADUser and the WhenCreated attribute to produce some custom properties via Select-Object and some hash tables. $Team is an array of my team members names. Let's take a closer look...

 

DaysOfService

The first key-value pair defines the name of the new property - DaysOfService. The second key-value pair is the expression arriving at the computed value of the property.

 
$((Get-Date)) - $($_.WhenCreated)).Days

 

The current date and time minus the WhenCreated date of time. The Days property of the resultant object is then displayed.

 

ServiceAnniversaryIn2016

The first key-value pair defines the name of the new property - ServiceAnniversaryIn2016. The year is a variable...

 
"ServiceAnniversaryIn$($(Get-Date).Year)"

 

The second key-value pair is the expression arriving at the computed value of the property.

 
$((Get-Date).Year) - $($_.WhenCreated.Year)

This is a simple current year minus WhenCreated year calculation to arrive at the Service Anniversary that will occur in the current year, i.e. if the code is executed in February and a team member is due their three year anniversary in November, the value of the property will be 3.

 

"...In theory one is aware that the earth revolves, but in practice one does not perceive it, the ground upon which one treads seems not to move, and one can live undisturbed. So it is with Time in one's life..."

Marcel Proust