Date time comparison issue

Hi
In order to disable leavers I need to check if the accountexpirationdate is less then or equal to today
I run the following if statement in a foreach block

$mainCsvPath = "C:\Users\Public\Documents\CSV\offboarding"
$withemployeeID = import-csv $maincsvpath\problem.csv
$date = (get-date)
$today = [DateTime]::ParseExact($date,'MM/dd/yyyy',$null)
foreach($Luser in $withemployeeID ){
     $AccountExpired =  [DateTime]::ParseExact($Luser.AccountExpirationDate,'MM/dd/yyyy',$null)

   
    $Employeenumber = $Luser.EmployeeNumber
      if(!($employeenumber -eq "")){
          if($accountexpired -eq ""){
              [PSCustomObject]@{
                  EmployeeNumber = $Luser.EmployeeNumber    
                  SamAccountName = $Luser.SamAccountName
                  EmailAddress = $Luser.EmailAddress
                  UserPrincipalName = $Luser.UserPrincipalName
                  AccountExpirationdate = $accountexpired
                  Manager = $Luser.Manager
                  Treatmentdate = $today
                  } | Export-csv -path $mainCsvPath\incorrectdata.csv -notype -Append  
          }Else{
           
              if($accountexpired -lt $today){
                  $AccountExpired = $Luser.AccountExpirationdate
                  $accountexpired = (get-date $accountexpired).AddDays(-1)
                  $accountexpired = (get-date $accountexpired).tostring("yyyy-MM-dd")
                  [PSCustomObject]@{
                  EmployeeNumber = $Luser.EmployeeNumber    
                  SamAccountName = $Luser.SamAccountName
                  EmailAddress = $Luser.EmailAddress
                  UserPrincipalName = $Luser.UserPrincipalName
                  AccountExpirationdate = $accountexpired
                  Manager = $Luser.Manager
                  Treatmentdate = $today
                  } | Export-csv -path $mainCsvPath\leaversFinal.csv -notype -Append
              }Else{

                if($accountexpired -eq $today){
                  $AccountExpired = $Luser.AccountExpirationdate
                  $accountexpired = (get-date $accountexpired).AddDays(-1)
                  $accountexpired = (get-date $accountexpired).tostring("yyyy-MM-dd")
                  [PSCustomObject]@{
                  EmployeeNumber = $Luser.EmployeeNumber    
                  SamAccountName = $Luser.SamAccountName
                  EmailAddress = $Luser.EmailAddress
                  UserPrincipalName = $Luser.UserPrincipalName
                  AccountExpirationdate = $accountexpired
                  Manager = $Luser.Manager
                  Treatmentdate = $today
                  } | Export-csv -path $mainCsvPath\leaversFinal.csv -notype -Append
              }
          }
        }
    }

  }

my csv looks like this 

“EmployeeNumber”,“SamAccountName”,“EmailAddress”,“UserPrincipalName”,“AccountExpirationDate”,“Manager”,“Treatmentdate”
“600027",“samaccountname”,"user@mydomain.com”,"user@mydomain.com",“10/30/2022 12:00:00 AM”,“myManager”,"

but when I run the code it does not validate the if statement ($accountexpired -lt $today) as being True what am I doing wrong here?

Don’t you get error messages running this code? :thinking: Even when I switch my language/culture setting to en-GB or en-US I get errors.

Anyway … the way you use [DateTime] types looks weird to me. If you want to compare these types by date without worrying about the time part you set the time part to 0 … like this

(Get-Date).Date

If you do the same to the input string from your CSV file you should be able to get correct comparison results:

(Get-Date '10/30/2022 12:00:00 AM').Date

If you want to manipulate these dates and output them in a certain string format you can do that with one line. You don’t need multiple variable assignments

(Get-Date '10/30/2022 12:00:00 AM').Date.AddDays(-1).ToString('yyyy-MM-dd')
1 Like

Hi olaf,
Yes I do get errors and I try to eliminate them :smile:
thanks for the tip on the get date.

another question as you might see in my code how can I optimize my excisting code in a correct way are there websites tutorials available to do this?

So you should share them completely as well (formatted as code as well) along with the code you used. Error messages are an important language feature telling you what’s wrong and sometimes even how to do it better.

It depends pretty much on what optimization you want. Of course you should debug errors if you have some. Apart from that - if it does the job … :man_shrugging:t4:

If you’re unsatisfied with your own code you may identify the parts bothering you and work on that.

Again - it depends on what you want to improve … maybe this: