Introduction

Managing Active Directory (AD) users efficiently is a crucial task for IT administrators. Often, organizations require bulk updates to user attributes such as names, display names, and more. This blog will guide you through using PowerShell to update AD user details, including handling common errors.

Prerequisites

Before executing the script, ensure that:

  • You have administrative privileges on the AD domain.
  • The Active Directory module for PowerShell is installed.
  • The CSV file containing user details is formatted correctly.

Preparing the CSV File

Create a CSV file (e.g., userdata.csv) with the following headers:

SamAccountName,FirstName,LastName
john.doe,John,Doe
jane.smith,Jane,Smith

This file contains the SamAccountName (username), FirstName, and LastName of each user to be updated.

PowerShell Script for Updating AD Users

Use the following PowerShell script to update GivenName, Surname, DisplayName, and Name attributes:

# Import Active Directory module
Import-Module ActiveDirectory

# Define CSV file path
$CSVFilePath = "c:\PS\userdata.csv"

# Import the CSV file
$Users = Import-Csv -Path $CSVFilePath

foreach ($User in $Users) {
    # Extract user attributes from CSV
    $SamAccountName = $User.SamAccountName
    $FirstName = $User.FirstName
    $LastName = $User.LastName
    $DisplayName = "$FirstName $LastName"

    # Check if the user exists in AD
    $ADUser = Get-ADUser -Filter {SamAccountName -eq $SamAccountName} -Properties DistinguishedName -ErrorAction SilentlyContinue
    
    if ($ADUser) {
        # Update AD user attributes
        Set-ADUser -Identity $SamAccountName `
            -GivenName $FirstName `
            -Surname $LastName `
            -DisplayName $DisplayName

        # Update the "Name" attribute using Rename-ADObject
        Rename-ADObject -Identity $ADUser.DistinguishedName -NewName $DisplayName

        Write-Host "Updated: $SamAccountName - $DisplayName" -ForegroundColor Green
    } else {
        Write-Host "User not found: $SamAccountName" -ForegroundColor Red
    }
}

Explanation of the Script

  1. Import Active Directory Module – Ensures the necessary module is available.
  2. Import CSV Data – Reads the user details from userdata.csv.
  3. Loop Through Users – Iterates through each user and updates their attributes.
  4. Set-ADUser – Updates GivenName, Surname, and DisplayName.
  5. Rename-ADObject – Changes the Name (Common Name) attribute, which Set-ADUser cannot modify.
  6. Error Handling – Uses -ErrorAction SilentlyContinue to prevent script failure if a user is not found.

Common Errors and Solutions

1. The attribute cannot be modified because it is owned by the system

  • Cause: The Name attribute cannot be changed with Set-ADUser.
  • Solution: Use Rename-ADObject instead, as shown in the script.

2. User Not Found

  • Cause: The SamAccountName in the CSV does not match any AD user.
  • Solution: Verify the usernames in Active Directory using Get-ADUser.

3. Insufficient Privileges

  • Cause: The script is not run as an administrator.
  • Solution: Open PowerShell as an administrator.

Conclusion

This PowerShell script simplifies the process of updating user details in Active Directory. By leveraging PowerShell’s automation capabilities, administrators can efficiently manage large-scale AD modifications with minimal effort. If you have any questions or need further customization, feel free to comment below!

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *