Forum Discussion

Twrriglesworth's avatar
Twrriglesworth
Copper Contributor
May 03, 2024
Solved

Repeating a task

Hi all,    We are currently using Power Automate to create on-prem users. The problem we've hit is that the UPN suffix is blank (different issue).   To get around this, I've made a PowerShell scr...
  • Harm_Veenstra's avatar
    May 12, 2024

    Twrriglesworth If you get more than one user who needs changing, it stops because you don't loop through them. I changed the script and tested it in my environment, works 🙂 

     

    foreach ($user in Get-ADUser -Filter { UserPrincipalName -notlike '*@*' } -Properties UserPrincipalName -ResultSetSize $null) {
        $UPN = $User.UserPrincipalName + "@tww-lab.co.uk"
        try {
            Set-ADUser -Identity $User.SamAccountName -UserPrincipalName $UPN -ErrorAction Stop
            Write-Host ("Changed UPN of User {0} to {1}" -f $user.name, $upn) -ForegroundColor Green
        }
        catch {
            Write-Warning ("Error changing UPN of User {0} to {1}, check permissions! Skipping...")
        }
    }



    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.

    If one of the posts was helpful in other ways, please consider giving it a Like.

Resources