Script Repository


Capitalize first letter in each word of a text property

February 25, 2021
465

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 ( 2 )
avatar
jay
Apr 12, 2022
This script completes but does not changes first or last name properties.

$propertyName = @("givenName", "sn") # TODO: modify me
avatar
Support
Apr 13, 2022
Hello Jay,

The script is not intended to work with multiple properties. Specifying an array in the $propertyName variable will not work. Do we understand correctly that you need the script to process all the properties you specify in the variable same way as it currently works for a single property?
Leave a comment