User Tools

Site Tools


actualizar_los_upn_de_un_grupo_de_userprincipalnames

Esto se puede correr en el DC para tener acceso al módulo de AD. Reemplaza el UPN viejo por el nuevo. El csv empieza con oldUPN, newUPN

Yes, it is possible to provide a CSV file with two columns, one for the old UPNs and one for the corrected UPNs. You can then use this CSV file to update the User Principal Names (UPNs) in Active Directory.

Here's an example script that demonstrates how you can achieve this:

update_UPN_ADUsers.ps1
# Import the Active Directory module
Import-Module ActiveDirectory
 
# Path to the CSV file
$csvPath = "C:\path\to\your\csvfile.csv"
 
# Read the CSV file
$csvData = Import-Csv -Path $csvPath
 
# Iterate through each entry in the CSV and update the UPNs
foreach ($entry in $csvData) {
    $oldUPN = $entry.oldUPN
    $newUPN = $entry.newUPN
 
    # Get the user account based on the old UPN
    $user = Get-ADUser -Filter "UserPrincipalName -eq '$oldUPN'"
 
    # Check if the user account exists
    if ($user) {
        # Update the UPN for the user
        Set-ADUser -Identity $user.SamAccountName -UserPrincipalName $newUPN
        Write-Host "Updated UPN for user $($user.SamAccountName)"
    } else {
        Write-Host "User with UPN '$oldUPN' not found"
    }
}

In this script, you specify the path to your CSV file using the `$csvPath` variable. The CSV file should have two columns: `oldUPN` for the old UPNs and `newUPN` for the corrected UPNs.

The script then uses the `Import-Csv` cmdlet to read the CSV file and stores the data in the `$csvData` variable.

Next, it iterates through each entry in the CSV using a `foreach` loop. Inside the loop, it retrieves the `oldUPN` and `newUPN` values from each entry.

Then, it uses the `Get-ADUser` cmdlet to find the user account based on the old UPN. If the user account exists, it uses the `Set-ADUser` cmdlet to update the UPN with the new value.

Finally, it displays a message indicating that the UPN has been updated for each user.

Make sure to adjust the `$csvPath` variable to the actual path of your CSV file.

Please note that you need to have the Active Directory module installed on the machine where you're running this script. Additionally, ensure that you have the necessary permissions to modify user accounts in Active Directory.

Make sure to test this script in a controlled environment or with a limited set of user accounts before running it in a production environment.

CSV de ejemplo:

oldUPN,newUPN
10956 @example.com.ar,[email protected]
10958 @example.com.ar,[email protected]
10999 @example.com.ar,[email protected]
11000 @example.com.ar,[email protected]
11065 @example.com.ar,[email protected]
11133 @example.com.ar,[email protected]
11239 @example.com.ar,[email protected]
11323 @example.com.ar,[email protected]
actualizar_los_upn_de_un_grupo_de_userprincipalnames.txt · Last modified: 2024/10/17 21:42 by 127.0.0.1