1

I have an array of staff data($employees) and in that array their is a field called start_date which is listed as a string. I am trying to filter out any staff member who's start_date is equal to the current month. Initially I thought I could had just wrote:

$employee | Where-Object {$_.start_date -ge Get-Date()}

Which didn't work.

I then tried to convert the Get-Date to a string with: 
$date = Get-Date -f '01/MM/yyyy'
$dateString = $date.toString

$employee | Where-Object {$_.start_date -ge $dateString}

Which also did not give me the results I was after also.

My Colleague who is fairly business with his work states, I might be best writing a foreEach-Object but I have always struggled how to write this, and don't wish to pull him away from his work to assist me on this task.

Example Array:

preferred_name    : John
name_suffixes     :
gender            : M
date_of_birth     : 09/05/1596
surname           : Doe
supervisor        : PRNMA01
start_date        : 28/11/2020
school_email      : [email protected]
teacher_code      :
termination_date  :
employee_photo    : @{file_info=}
supervisor_2      :
title             : Mr
driver_licence_no :
supplier_code     :
given_names       : John Jane

Task: Get the list of new staff and email the list to the department who looks after new staff training.

Any help on this would be good. Please ensure you explain in detail as i am not very bright in this field.

Thanks in advance.

1 Answer 1

2

You can convert the start_date into datetime using parseexact and then compare the datetime objects directly.

$employee | Where-Object {[datetime]::parseexact($_.start_date, 'dd/MM/yyyy', $null) -ge (Get-Date)}
Sign up to request clarification or add additional context in comments.

4 Comments

Looks like that was a no go. It gave me results back that were not Equal to the current month as you can see below: supervisor :--- start_date : 16/12/2019 school_email : ----------- teacher_code : termination_date : employee_photo : @{file_info=} supervisor_2 : title : ----------- driver_licence_no : supplier_code : given_names : -------
@FallenSoul Updated my answer!
Still not quiet there. its not showing any from this month still. just for next year "start_date : 03/01/2022" If you look at the script that is currently running (uses sql but we are moving to API) is has this filter: so basically it needs to be any staff member who started between 01/Month/Year to 31/Month/Year Month(dateadd (Month, 0, Getdate())) = Month(start_date) and Year(Getdate()) = Year(start_date)
I was able to resolve this by adding brackets around ([System.DateTime]::parseexact($_.start_date, 'dd/MM/yyyy', $null) So the full code line now looks like : $employees | Where-Object {([System.DateTime]::parseexact($_.start_date, 'dd/MM/yyyy', $null).Month -ge (Get-Date).Month) -and $_.employment_status -eq 'f'} | Select-Object preferred_name, surname, school_email

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.