the only thing you should change is the $ou variable to represent the Searchbase.
In this example the searchbase goes 1 OU deep but you can change that ofcourse by setting another LDAP.
$ou = "<OU=Name of OU>,DC=<domainname>,DC=<extention>"
$users = (get-aduser -searchbase $ou -filter * | select -ExpandProperty Samaccountname)
foreach ($user in $users)
{
Set-ADUser $user -UserPrincipalName (Get-ADUser $user -Properties mail | Select-Object -ExpandProperty mail)
}