Hello,
I noticed when running in a loop with multiple domains it will fail. I solved this by commenting out the lines below as they will change the context from the provided $domain variable to actually pulling the current domain in the context of the powershell.exe window.
$DirEntry = New-Object System.DirectoryServices.DirectoryEntry
$UserSearcher.SearchRoot = $DirEntry