Bulk Change AD User Login Name with powershell
# Active Directory Module Install
#Import-Module ActiveDirectory
# CSV file:
$kullanicilar = Import-Csv -Path "C:\news.csv"
# Domain name:
$domain = "guler.com"
# Logon Name Update:
foreach ($kullanici in $kullanicilar) {
$oldUsername = $kullanici.old_username
$newLogonName = $kullanici.new_username
if ($oldUsername -and $newLogonName) {
try {
# User find and update:
$user = Get-ADUser -Identity $oldUsername -ErrorAction Stop
$newUserPrincipalName = "$newLogonName@$domain"
Set-ADUser -Identity $user -UserPrincipalName $newUserPrincipalName -SamAccountName $newLogonName -ErrorAction Stop
Write-Host "$oldUsername için $newUserPrincipalName olarak güncellendi."
} catch {
Write-Host "$oldUsername için hata oluştu: $_"
}
} else {
Write-Host "Hata: Boş veya geçersiz kullanıcı adı tespit edildi. old: '$oldUsername', new: '$newLogonName'"
}
}
When you change a user's samAccountName in Active Directory, the user's SID (Security Identifier) and GUID (Globally Unique Identifier) do not change.
At the end of this process the following Attributes will be changed:
- msDS-Principalname
- sAMAccountName
- userPrincipalName
- mailNickname(*If there is an Exchange)
This post is licensed under CC BY 4.0 by the author.