Script repository

Add computers located in an OU to user workstations

Updated on: Jan 18, 2026, Views: 4916

Computers, User accounts

The script adds all computers located in a specific Organizational Unit to user workstations. Current computers in user workstations will remain unchanged. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.

In the script, the $OUDN variable specifies the distinguished name (DN) of the Organizational Unit to search computers in. For information on how to get the DN, see Get the DN of a directory object.

$OUDN = "OU=Computers,DC=domain,DC=com" #TODO: modify me

# Get current logon workstations
try
{
    $logonWorkstations = $Context.TargetObject.Get("userWorkstations")
}
catch
{
    $logonWorkstations = ""
}

# Search parameters
$searcher = $Context.BindToObjectByDN($OUDN)
$searcher.Criteria = New-AdmCriteria "computer" -Expression {dNSHostName -empty $False}
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.PageSize = 500

try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    if ($searchResults.Length -eq 0)
    {
        $Context.LogMessage("No computers were found.", "Information")
        return
    }
    
    foreach ($searchResult in $searchResults)
    {
        # Get computer DNS host name.
        $computerDNSHostName = $searchResult.GetPropertyByName("dNSHostName").Values[0]        
        
        # Update workstations list.
        if ($logonWorkstations -notlike "*$computerDNSHostName*")
        {
            $logonWorkstations = $logonWorkstations + "," + $computerDNSHostName
        }             
    }
    
    # Update the user
    $Context.TargetObject.Put("userWorkstations", $logonWorkstations)
    $Context.TargetObject.SetInfo()
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

Comments 0

You must be signed in to comment.

    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.