Remove Object from Array

I have an array which contain the LastLogonDate (OnPrem AD), the EntraID LastSignIn and the EntraID LastNonInterActiveSignIn. I am comparing these three dates to filter out accounts which have logged in for the last 90 days.

foreach ($Obj in $ToDisableFilter) {
    $Dates = $Obj.EntraID_LastSignin, $Obj.LastLogonDate, $Obj.EntraID_LastNonInteractive | Sort-Object -Descending
    $LastSignInDate = $Dates[0]
    If ($LastSignInDate -lt $DisableDate) {
        Write-Log -Message "$($Obj.Name) Last sign in date $($LastSignedInDate) is older then $($DisableDate), wherefore the user will be disabled" -Severity Information
        Disable-ADAccount -Identity $Obj.DistinguishedName
    } Else {
        Write-Log -Message "$($Obj.Name) will not be disabled, because $($LastSignInDate) is newer then $($DisableDate)" -Severity Information
        $ToDisableFiltered = $ToDisableFilter | Where-Object {$_.Name -notcontains $Obj.Name}
    }
}

The log clearly shows that the items which need to filtered out are successfully retrieved, however the Array “$ToDisableFiltered” still contains some objects that should be filtered out.
LogFile:

"18/10/2024 10:56","Benjamin Beulens will not be disabled, because 17/10/2024 18:09:38 is newer then 08/19/2024 10:55:10","Information"
"18/10/2024 10:56","Chris Isaac Last sign in date  is older then 08/19/2024 10:55:10, wherefore the user will be disabled","Information"
"18/10/2024 10:56","Dominique Lessage Last sign in date  is older then 08/19/2024 10:55:10, wherefore the user will be disabled","Information"

Array

Name                       : Benjamin Beulens
EntraID_DisplayName        : Benjamin Beulens
Mail                       : Benjamin Beulens@talktalk.back.com
CreationDate               : 08/05/2024 14:16:03
LastLogonDate              : 08/07/2024 08:12:50
DistinguishedName          : CN=Benjamin Beulens,OU=UserContainer,DC=talktalk,DC=internal
EntraID_LastSignin         : 17/10/2024 12:34:39
EntraID_LastNonInteractive : 17/10/2024 18:09:38
AccountType                : Hybrid
GivenName                  : Benjamin
SurName                    : Beulens
AdminDescription           : 
UserType                   : Member
Manager                    : CN=Run DMC,OU=UserContainer,DC=talktalk,DC=internal
Managers_Email             : Run.DMC@talktalk.back.com
Managers_GivenName         : Run
Managers_SurName           : DMC

Name                       : Joel Smets
EntraID_DisplayName        : Joel Smetst
Mail                       : Joel.Smets@talktalk.back.com
UserPrincipalName          : Joel.Smets@talktalk.back.com
EntraID_UPN                : Joel.Smets@talktalk.back.com
CreationDate               : 03/07/2024 11:56:54
LastLogonDate              : 03/07/2024 13:53:38
DistinguishedName          : CN=Joel Smets,OU=UserContainer,DC=talktalk,DC=internal
EntraID_LastSignin         : 04/07/2024 13:16:53
EntraID_LastNonInteractive : 04/07/2024 13:41:51
AccountType                : Hybrid
GivenName                  : Joel
SurName                    : Smets
AdminDescription           : 
UserType                   : Member
Manager                    : Not Present
Managers_Email             : philippe.Gilbert@talktalk.back.com
Managers_GivenName         : Philippe
Managers_SurName           : Gilbert

Name                       : Dominique Lessage
EntraID_DisplayName        : Dominique Lessage
Mail                       : Dominique.Lessage@talktalk.back.com
UserPrincipalName          : Dominique.Lessage@talktalk.back.com
EntraID_UPN                : Dominique.Lessage@talktalk.back.com
CreationDate               : 03/07/2024 12:00:31
LastLogonDate              : 15/07/2024 09:09:17
DistinguishedName          : CN=Dominique Lessage,OU=UserContainer,DC=talktalk,DC=internal
EntraID_LastSignin         : 17/10/2024 12:12:09
EntraID_LastNonInteractive : 18/10/2024 04:52:59
AccountType                : Hybrid
GivenName                  : Dominique
SurName                    : Lessage
AdminDescription           : 
UserType                   : Member
Manager                    : Not Present
Managers_Email             : 
Managers_GivenName         : 
Managers_SurName           : 

What’s your question?
Are you saying that “array” is the output of $ToDisableFiltered?
Can you explain the logic behind this?

 $ToDisableFiltered = $ToDisableFilter | Where-Object {$_.Name -notcontains $Obj.Name}

Maybe I haven’t had enough coffee but I’m not understanding the reasoning here.

Also, maybe they’re just example dates but what format are your datestrings in? is 17/10/2024 = October 17th?

I need to remove the users from the array which have logged on within 90days.
Therefore i am trying to remove the user from the array by this:

$ToDisableFiltered = $ToDisableFilter | Where-Object {$_.Name -notcontains $Obj.Name}

The logging clearly shows that the correct objects are targeted, so the date seems to be correct.

AccountType                NoteProperty string AccountType=Hybrid
AdminDescription           NoteProperty object AdminDescription=null
CreationDate               NoteProperty datetime CreationDate=10/01/2022 13:42:53
DistinguishedName          NoteProperty string DistinguishedName=
EntraID_DisplayName        NoteProperty System.String EntraID_DisplayName=adm_rschoovaerts
EntraID_LastNonInteractive NoteProperty System.DateTime EntraID_LastNonInteractive=14/10/2024 10:45:16
EntraID_LastSignin         NoteProperty System.DateTime EntraID_LastSignin=14/10/2024 10:44:33
EntraID_UPN                NoteProperty System.String EntraID_UPN=xxxxxxx@xxxxxxxxxxx.xxx
GivenName                  NoteProperty string GivenName=xxxxxxx
LastLogonDate              NoteProperty datetime LastLogonDate=30/07/2024 15:33:28
Mail                       NoteProperty object Mail=null
Manager                    NoteProperty string Manager=
Managers_Email             NoteProperty string Managers_Email=xxxxxxxxxxxxxxxx@xxxxx.xxx
Managers_GivenName         NoteProperty string Managers_GivenName=xxxxxx
Managers_SurName           NoteProperty string Managers_SurName=xxxxxxx
Name                       NoteProperty string Name=xxxxxxxxxxx
SurName                    NoteProperty string SurName=xxxxxxxxxxxx
UserPrincipalName          NoteProperty string UserPrincipalName=xxxxxxxxxx@xxxxxxxxx.xxx
UserType                   NoteProperty string UserType=Member

All dates seem to be DateTime properties

I wanted to keep my old array intact, wherefore i used a new array:

 $ToDisableFiltered = $ToDisableFilter | Where-Object {$_.Name -notcontains $Obj.Name}

But that doesn’t work, but this does:

$ToDisableFilter1 = $ToDisableFilter1 | Where-Object {$_.Name -ne $Obj.Name}

Why is that?

Because -NotContains works against arrays, not strings.