functions/Format-DisplayNames.ps1

function Format-DisplayNames {

        <#
    .SYNOPSIS
        This function will format display names for all users in the selected OU
        It will ensure display names are "Firstname Lastname" with correct capitalization
        It bases this off of the first and last name attributes of the users
      
      
    .NOTES
        Name: Format-DisplayNames
        Author: Elliott Marter
      
      
    .EXAMPLE
        Format-DisplayNames
      
      
    .LINK
        https://www.powershellgallery.com/profiles/elliottmarter
    #>


    [cmdletbinding()]
    param ()

    # get users in a certain OU
    $OU = Select-ADOrganizationalUnit -HideNewOUFeature | Select-Object DistinguishedName -ExpandProperty DistinguishedName
    $Users = Get-ADUser -Filter * -SearchBase $OU

    # loop through all users found
    foreach($U in $Users){

        # assign variables
        $firstname = $U.givenname
        $surname = $U.surname
        $olddisplayname =$U.name
        
        # reassign variables with correct capitalization
        $firstname = $firstname.substring(0,1).ToUpper()

        $surname = $surname.substring(0,1).ToUpper()+$surname.substring(1).ToLower()

        # create the correct displayname
        $newdisplayname = $firstname + " $surname"

        # perform the rename action
        Set-ADUser -Identity $U -DisplayName $newdisplayname
        Rename-ADObject -Identity $U -NewName $newdisplayname

        Write-Host "Renamed $olddisplayname to $newdisplayname" -ForegroundColor Green

    }
}