Script repository

Users whose property values do not match property pattern

Updated on: Jan 18, 2026, Views: 3675

Reports

The script generates a report of users whose property values do not match constraints of effective property patterns. Also, the report will include users with empty properties that are mandatory according to effective property patterns. To execute the script, create a report with a scope including users that should be checked by the script.

function IsUserPropertiesValid($propertyPatternDN, $user)
{
    # Bind to the property pattern.
    $propertyPattern = $Context.BindToObjectByDN($propertyPatternDN)

    # Get all properties from the property pattern to load.
    $propertiesToLoad = @()
    foreach($item in $propertyPattern.Items)
    {
        $propertiesToLoad += $item.PropertyName
    }
    
    $user.GetInfoEx($propertiesToLoad, 0)
    $userPropertyList = $user.PropertyList

    foreach($item in $propertyPattern.Items)
    {
        # Password isn't retrievable
        if($Item.PropertyName -eq 'unicodePwd')
        {
            continue
        }
        
        # Get property entry.
        try
        {
            $propertyEntry = $userPropertyList.Item($item.PropertyName)
        }
        catch
        {
            if ($item.IsPropertyRequired)
            {           
                return $False
            }
            else
            {
                continue
            }
        }
        
        if ($item.IsPropertyRequired -and ($propertyEntry.Values.Length -le 0))
        {            
            return $False
        }        
        
        $propertyEntry.ControlCode = "ADS_PROPERTY_UPDATE"
        
        # Get constraints
        $constraints = $item.GetConstraints()
        foreach($constraint in $constraints)
        {
            $errorMsg = $NULL
            if ($constraint.Check($propertyEntry, $user, [ref]$errorMsg))
            {
                continue
            }
            return $False
        }
    }
    
    return $True
}

try
{
    $Context.DirectorySearcher.AppendFilter("(sAMAccountType=805306368)")
    $searchIterator = $Context.DirectorySearcher.ExecuteSearch()
    while ($Context.MoveNext($searchIterator))
    {
        $user = $Context.BindToObjectBySearchResult($searchIterator.Current)
        
        # Get property patterns effective for the user.
        try
        {
            $propertyPatternDNs = $user.GetEx("adm-EffectivePropertyPatterns")
        }
        catch
        {
            continue
        }
      
        $user.GetInfo()
        foreach($propertyPatternDN in $propertyPatternDNs)
        {
            if (IsUserPropertiesValid $propertyPatternDN $user)
            {
                continue
            }
            
            $Context.Items.Add($user)
            break
        }
    }
}
finally
{
    if ($searchIterator) { $searchIterator.Dispose() }
}

Comments 2

You must be signed in to comment.

  • Daniel Gallop

    Daniel Gallop

    I'd recommend adding a check for the pattern name unicodePwd. The password can't be retrieved and checked, so that condition will always fail.

    foreach($item in $propertyPattern.Items)
    {
        # Password isn't retrievable
        if($Item.PropertyName -eq 'unicodePwd')
        {
            continue
        }
        
        # Get property entry.
    
    • Support

      Support

      Hello Daniel,

      Thank you for pointing out the behavior. We updated the script accordingly.

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.