Forum Discussion

JaneDo's avatar
JaneDo
Copper Contributor
Dec 07, 2022

PowerShell script to change UN Email mailnickname via CSV

I need to shorten M365 usernames to work with an app.  Azure cloud only environment. This requires that the Username, EmailAddress, and mailNickName all be changed. Since I need to do this for only1/3 of our users, I need to use a CSV file. Anyone able to help with this, please?

1 Reply

  • JaneDo's avatar
    JaneDo
    Copper Contributor
    I can't take credit for this, but in case anyone else needs it.

    #Connect to MSOLService with administrator at the console to provide credentials
    #Script will iterate through a CSV that contains OldUPN and NewUPN column headers
    #Set-MsolUserPrincipalName will change the UPN, the Email address, and mailNickname fields as requested

    Connect-Msolservice

    #Import CSV
    $ImportPath = Read-Host "Enter the path for the CSV, please ensure it has columns with headers named OldUPN and NewUpn with no spaces"
    $ImportFile = Import-CSV -Path "$ImportPath" -Delimiter ","

    #Iterate through the CSV
    foreach ($Import in $ImportFile) {
    $OldName = $import.OldUPN
    $NewName = $import.NewUPN
    Write-Host "Changing UPN value from $($OldName) to $($NewName)" -ForegroundColor Yellow
    Set-MsolUserPrincipalName -UserPrincipalName "$OldName" -NewUserPrincipalName "$NewName"
    }

Resources