How to Get-AdUser Whose description is not equal to Title

Yan, Jayden 81 Reputation points
2021-11-18T08:12:26.72+00:00

How to Make a PowerShell script, report every user account whose Description is not equal to Title, report the list to a CSV file for reporting. And then make the script update the user account in such a situation to match their Description field to their Title.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,898 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Clément BETACORNE 2,031 Reputation points
    2021-11-18T10:23:57.167+00:00

    Hello,

    Below a script that can help, I've tested it.
    It will export a list of users before the modification and you will have a status column which will tell you if the modification was successful:

    $colExportUser = @()
    $users = Get-ADUser -Filter * -Properties Description,Title | Where-Object {$_.Description -ne $_.Title}
    
    foreach($user in $users) {
        try {
            Set-ADUser -Identity $user.SamAccountName -Title $user.Description -ErrorAction Stop
            $hash = @{
                Name = $user.Name
                SamAccountName = $user.SamAccountName
                Enabled = $user.Enabled
                Description = $user.Description
                Title = $user.Title
                DistinguishedName = $user.DistinguishedName
                OperationStatus = $true
            }
    
            $objExportUser = New-Object PSObject -Property $hash
            $colExportUser = $colExportUser + $objExportUser
        }catch {
            $hash = @{
                Name = $user.Name
                SamAccountName = $user.SamAccountName
                Enabled = $user.Enabled
                Description = $user.Description
                Title = $user.Title
                DistinguishedName = $user.DistinguishedName
                OperationStatus = $false
            }
    
            $objExportUser = New-Object PSObject -Property $hash
            $colExportUser = $colExportUser + $objExportUser
        }
    }
    
    $colExportUser | Export-Csv -Delimiter ";" -Path <yourpath> -NoTypeInformation
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. SChalakov 10,261 Reputation points MVP
    2021-11-18T09:50:26.907+00:00

    Hi @Yan, Jayden ,
    this should meet the first requirement:

    $UserstoEdit = Get-ADUser -SearchBase "DC=domain,DC=com" -Filter {Enabled -eq $true} -Properties * | Where-Object ($_.Description -Notlike $_.Title) | Export-CSV c:\temp\aduser.csv -NoTypeInformation  
      
    foreach ($SingleUser in $UserstoEdit) {  
      
        Set-ADUser $SingleUser.SamAccountName -Description $SingleUser.Title  
    }  
    

    Unfortunately I am currently not able to test and although it should be working I would encourage to test in a test environment first.

    Hope that helps!

    ----------

    If my reply was helpful please don't forget to upvote and/or accept as answer, thank you!
    Regards,
    Stoyan

    0 comments No comments