Script repository

Check if account is inactive in Exchange Online longer than a period of time

Updated on: Jan 18, 2026, Views: 1395

Exchange

The script returns true if the account is inactive in Exchange Online longer than a period of time. To execute the script, use the If PowerShell script returns true condition in a business rule, custom command or scheduled task configured for the required object type.

In the script, the $inactivityDurationThreshold variable specifies the inactivity duration (in days) that should be exceeded for the condition to be met.

$inactivityDurationThreshold = 45 # Days. TODO: modify me

# Check recipient type and location.
$Context.ConditionIsMet = $False
if ($Context.TargetObject.RecipientLocation -ne "ADM_EXCHANGERECIPIENTLOCATION_EXCHANGEONLINE" -or
    $Context.TargetObject.RecipientType -ne "ADM_EXCHANGERECIPIENTTYPE_MAILBOXENABLED")
{
   return
}

# Get last logon date from Exchange.
$mailboxParams = $Context.TargetObject.GetMailParameters()
$lastLogonDate = $mailboxParams.UsageInfo.LastLogonDate

if ($lastLogonDate -eq [DateTime]::MinValue)
{
    return
}

$date = [System.Datetime]::Now.AddDays(- $inactivityDurationThreshold)
$Context.ConditionIsMet = $lastLogonDate -lt $date

Comments 2

You must be signed in to comment.

  • TB

    TB

    Hello,
    The script return this error :
    The property 'ConditionIsMet' cannot be found on this object. Verify that the property exists and can be set. Trace de pile: at , : line 9

    Regards,
    TB

    • Support

      Support

      Hello,

      The error occurs because you are using the script in the Run a program or PowerShell script action. The ConditionIsMet property is only available in script conditions. As it is specified in the script description, it can only be used in the If PowerShell script returns true condition.

Got questions?

Support Questions & Answers

We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.