powershellactive-directory

Having trouble making UserPrincipalName all lowercase in Powershell


I am trying to run a PowerShell script to change all UPNs in a specific OU to lowercase, but I am not having any success. Based on some previous posts and using the same command I use to change other attributes successfully, I am trying the following:

Get-ADUser -Filter * -SearchBase "OU=[REDACTED]" 
    | Set-ADUser -replace @{userPrincipalName=userPrincipalName.ToLower}

But when I run this I am getting an error

'userPrincipalName.ToLower' is not recognized

I have also tried adding () to the end of ToLower, but with the same result.

Any suggestions would be greatly appreciated. Thank you for your time.


Solution

  • You're missing a loop in your code to get the pipped user's UserPrincipalName, add ForEach-Object and reference it via $_.UserPrincipalName, then you can .ToLower() it:

    Get-ADUser -Filter * -SearchBase 'OU=[REDACTED]' | ForEach-Object {
        Set-ADUser -Identity $_ -UserPrincipalName $_.UserPrincipalName.ToLower()
    }