Author Posts

April 13, 2016 at 10:06 am

I need to rename all domain accounts (samaccountname) and UPN with the data I will be provided with an excel (.csv) spreadsheet. SamAccount UPN prefix and name will be the same (xyz123@company.com). I am a newbie powershell script and not familiar with PS syntax.

I created a .csv file titled "UserList.csv" within the same folder as my .ps1 script. Within the .csv file, I have 3 columns: UserID,sAMAccountName, UserPrincipalName.

The UserID column has the user logon names for the users. The sAMAccountName and UserPrincipalName columns have what the company would like the new user logons names to be.

I want to use Get-ADuser cmlet to export a list of all Domain users to CSV file and then try Set-ADuser after.

Example:
Import-Module ActiveDirectory
$UserList = Import-CSV C:\scripts\UserList.csv
ForEach ($User in UserList)
{
Get-ADUser -Filter "sAMAccountName -eq '$($User.UserID)'"
-SearchBase 'OU=Users,DC=atciso,DC=lmco,DC=com'
}

Am I on the right track?

Thanks

April 13, 2016 at 10:27 am

I think you will also need to use Rename-ADobject

April 13, 2016 at 1:41 pm

Set-Aduser is what you are after.
$userupdate = Get-ADUser -Identity $User.SamAcccountName
$userupdate.SAMAccountName = $User.NEWSamAcccountName, etc.
Set-ADUser -Instance $userupdate

April 13, 2016 at 11:54 pm

I think this will work. You might need to tweak:

$UserList = Import-CSV C:\scripts\UserList.csv

ForEach ($User in $UserList)
{
    $csvusername = $user.samaccountname
    $csvuserprincipalname = $user.userprincipalname
    Get-ADUser $($user.Userid) | Set-Aduser -SamAccountName $csvusername -      
    UserPrincipalName $csvuserprincipalname
}

April 14, 2016 at 9:02 am

Thank you Wei-Yen Tan. That did it.