# Comparing dates

**URL:** <https://forums.powershell.org/t/comparing-dates/23850>\
**Category:** PowerShell Help\
**Created:** [March 25, 2024, 3:09pm UTC](https://forums.powershell.org/t/comparing-dates/23850 "2024-03-25T15:09:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Killerbe](https://avatars.discourse-cdn.com/v4/letter/k/d6d6ee/32.png) [@Killerbe](https://forums.powershell.org/u/Killerbe)\
**Post date:** [March 25, 2024, 3:09pm UTC](https://forums.powershell.org/t/comparing-dates/23850/1 "2024-03-25T15:09:31Z")

</div>

What am i doing wrong?  
I need to get all users which did not sign in interactively on noninteractively in the last 425 days. For this i need to look at two attributes:

1. LastSignInDateTime
2. LastNonInteractiveSignInDateTime

I also want to filter out all objects where the invite has been sent this year.  
My arrays conains folliwing objects

```auto
PS C:\VSCode\PS> $UnfilteredStaleAccounts | Get-Member -MemberType Noteproperty | select Name

Name
----
CreatedDateTime
DisplayName
ExternalUserState
Id
LastNonInteractiveSignInDateTime
LastSignInDateTime

```

Is use following filter

```auto
$StaleGuests = $UnfilteredStaleAccounts | Where-Object {($_.LastSignInDateTime -lt (Get-Date).AddDays(-425)) -or ($_.LastNonInteractiveSignInDateTime -lt (Get-Date).AddDays(-425)) -and ($_.CreatedDateTime -notlike (Get-Date).Year)}

```

When i run the filter i get all objects where LastNonInteractiveSignInDateTime has a date, even dates newer then 425 days.

```auto
LastNonInteractiveSignInDateTime LastSignInDateTime
-------------------------------- ------------------
25/03/2024 09:41:59
25/03/2024 09:40:20
25/03/2024 09:38:01
25/03/2024 09:33:12
25/03/2024 09:33:06
25/03/2024 09:32:20
25/03/2024 09:31:46
25/03/2024 09:29:50
25/03/2024 09:29:37
25/03/2024 09:19:46
25/03/2024 09:17:54
25/03/2024 09:17:17
25/03/2024 09:13:56
25/03/2024 09:11:11

```

---

<div class="post-metadata">

**Author:** ![Alastor2262](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Alastor2262](https://forums.powershell.org/u/Alastor2262)\
**Post date:** [March 25, 2024, 4:40pm UTC](https://forums.powershell.org/t/comparing-dates/23850/2 "2024-03-25T16:40:23Z")

</div>

Hello,

You use “-or” and “-and” without explicite priority, I means, without parenthesis that put explicite priority of “-and” and “-or”.  
I think you may look at that !

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [March 25, 2024, 5:20pm UTC](https://forums.powershell.org/t/comparing-dates/23850/3 "2024-03-25T17:20:36Z")

</div>

> [@Killerbe](#):
>
> `($_.CreatedDateTime -notlike (Get-Date).Year`

Don’t think this part will ever be true  
(Get-Date).Year is an int  
CreatedDateTime is either a string or datetime object??? If it’s a datetime or converted to one then you could say

```auto
($_.CreatedDateTime.Year -notlike (Get-Date).Year)

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [March 25, 2024, 5:30pm UTC](https://forums.powershell.org/t/comparing-dates/23850/4 "2024-03-25T17:30:28Z")

</div>

> [@Killerbe](#):
>
> ```auto
> $StaleGuests = $UnfilteredStaleAccounts | Where-Object {($_.LastSignInDateTime -lt (Get-Date).AddDays(-425)) -or ($_.LastNonInteractiveSignInDateTime -lt (Get-Date).AddDays(-425)) -and ($_.CreatedDateTime -notlike (Get-Date).Year)}
> 
> ```

Simplify your approach. Instead of stringing together a more complicated where-object scriptblock, filter out the accounts that were created this year. It’s not only going to make it easier for you to write, but it’ll make it easier for someone else to read later down the road. You also should be careful using the notlike on the date like that. No reason to do that if you have a datetime object to begin with, you can simply check the Year property and see if it matches the current year, if it doesn’t you can exclude it. For example:

```powershell
$Users = Get-MGuser -all -Property ID, Mail, UserPrincipalName, Signinactivity,CreatedDateTime -Top 100
$CurrentYear = (Get-Date).Year
$UserList = $Users | Where-Object { $_.CreatedDateTime.Year -ne $CurrentYear}

```

This gets you at a better starting place.

---

<div class="post-metadata">

**Author:** ![Killerbe](https://avatars.discourse-cdn.com/v4/letter/k/d6d6ee/32.png) [@Killerbe](https://forums.powershell.org/u/Killerbe)\
**Post date:** [March 26, 2024, 10:25am UTC](https://forums.powershell.org/t/comparing-dates/23850/5 "2024-03-26T10:25:21Z")

</div>

I filtered the objects first on date as adviced:

```auto
$UnfilteredStaleAccounts = $UnfilteredStaleAccounts | Where-Object {$_.CreatedDateTime.year -ne $CurrentYear}

```

Then filtered the remaining results on the two conditions

```auto
$StaleGuests = $UnfilteredStaleAccounts | Where-Object {($_.LastSignInDateTime -lt (Get-Date).AddDays(-425)) -or ($_.LastNonInteractiveSignInDateTime -lt (Get-Date).AddDays(-425))}

```

But the array staleguest still contains objects which are newer then 425 days ago.  
I have checked the noteproperties, and both LastSignInDateTime and LastNonInteractiveSignInDateTime are listed as System.DateTime

```auto
PS C:\VSCode\PS> $staleguests | Get-Member

   TypeName: Selected.Microsoft.Graph.Beta.PowerShell.Models.MicrosoftGraphUser

Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CreatedDateTime NoteProperty datetime CreatedDateTime=04/08/2019 08:41:45
DisplayName NoteProperty string DisplayName=Burggraeve Marianne
ExternalUserState NoteProperty string ExternalUserState=Accepted
Id NoteProperty string Id=63e8bb22-7fc1-42ba-a25f-9e89be654e57
LastNonInteractiveSignInDateTime NoteProperty System.DateTime LastNonInteractiveSignInDateTime=25/03/2024 21:52:17
LastSignInDateTime NoteProperty object LastSignInDateTime=null

```

---

<div class="post-metadata">

**Author:** ![Killerbe](https://avatars.discourse-cdn.com/v4/letter/k/d6d6ee/32.png) [@Killerbe](https://forums.powershell.org/u/Killerbe)\
**Post date:** [March 26, 2024, 12:33pm UTC](https://forums.powershell.org/t/comparing-dates/23850/6 "2024-03-26T12:33:49Z")

</div>

Found my foolish mistake, it shouldn’t be ‘-or’ but ‘-and’.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [March 26, 2024, 6:21pm UTC](https://forums.powershell.org/t/comparing-dates/23850/7 "2024-03-26T18:21:29Z")

</div>

Yea alastor mentioned that my comment was an additional tip to help. I should have clarified that though apologies.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 25, 2024, 6:21pm UTC](https://forums.powershell.org/t/comparing-dates/23850/8 "2024-04-25T18:21:57Z")

</div>

Closed on April 25, 2024, 6:21pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
