Script Repository


Capitalize first letter in each word of a text property

February 25, 2021
184

The script makes sure that each word in a text property value starts with a capital letter and the rest are lower case. the script can be used in custom commands and scheduled tasks configured for the User object type or in business rules triggering after an operation (e.g. After updating a user).

Parameters:

  • $separators - Specifies the characters that are users to separate parts of the property value.
  • $propertyName - Specifies the LDAP name of the property to be updated.
Edit Remove
PowerShell
$separators = @("'", " ", "-") # TODO: modify me
$propertyName = "givenName" # TODO: modify me

function ToTitleCase ($string)
{
    $string = $string.ToLower()
    foreach ($separator in $separators)
    {
        $stringParts = $string.Split($separator)
        for ($i = 0; $i -lt $stringParts.Length; $i++)
        {
            $stringParts[$i] = $stringParts[$i].SubString(0, 1).ToUpper() + $stringParts[$i].SubString(1)
        }
        
        $string = [System.String]::Join($separator, $stringParts)
    }
    
    return $string
}

# Get property value
try
{
    $value = $Context.TargetObject.Get($propertyName)
}
catch
{
    return
}

# Update value
$value = ToTitleCase $value

# Set value
$Context.TargetObject.Put($propertyName, $value)
$Context.TargetObject.SetInfo()

Comments ( 0 )
No results found.
Leave a comment