AD-Remove character from displayName Attribute in AD

Ben H 1 Reputation point
2021-08-10T16:13:42.607+00:00

Hello,

We currently use below to add 1 to any accounts' displayName attribute that are in a quarantine OU waiting to be deleted. It is part of a larger script.

Get-ADUser -SearchBase “OU=Quarantined,OU=Accounts,DC=Contoso,DC=com” -Filter { displayName -notlike "1"} -Properties displayname |

         

ForEach-Object {Set-ADObject -Identity $.distinguishedName -Replace @{displayName="$("1" + $.displayname)"}}

For some users who do return I have devised a script to enable various areas but I cannot seem to remove 1 from the displayName attribute if it exists. Any help would be appreciated.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,008 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,416 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,186 Reputation points
    2021-08-10T18:12:00.507+00:00

    Try this:

    Get-ADUser -SearchBase "OU=Quarantined,OU=Accounts,DC=Contoso,DC=com" -Filter { displayName -like "1*"} -Properties displayname |
        ForEach-Object {
            $d = $_.displayName -replace "^1", ""
            Set-ADObject -Identity $_.distinguishedName -Replace @{displayName="$d"}
        }
    
    0 comments No comments