Replace character in AD description for all computer accounts

Posted on 2014-08-25
Last Modified: 2014-08-26
Good morning experts!

I am looking for a script (ps prefered) that will replace a character in computer accounts description in a particular OU.

Under MYCompany OU:

If ':' exists, replace it with '.'
If '/' exists, replace it with '.'
If '(' exists, replace it with ' ' (space)
If ')' exists, replace it with ' ' (space)

We have added many descriptions over the years that include these types of characters, but now are running some ps scripts that these characters seem to interfere with.  Periods seem safe, so we'd like to do a mass replace of all of the problem characters to replace them with either periods or spaces.

Your help is appreciated, as always!
Question by:Irrylyn
    LVL 28

    Assisted Solution

    Something like this should work for you:

    Import-Module activedirectory
    $servers = Get-ADComputer  -filter * | select -expand DistinguishedName  | % {
    $sfull = $_
    $sname =  ($_.split(',')[0] -replace "CN=").ToUpper()
    if ($sfull -like "*OU=MYCompany*")
    $newname = $sname -replace ':', '.' -replace '/', '.' -replace '\(', '-' -replace '\)', '-'
    write-host "updating $sname to $newname"
    if ($newname -ne $null)
    Rename-Computer -ComputerName $sname -NewName $newname -DomainCredential Domain01\Admin01 -Force -Restart -WhatIf

    Open in new window

    LVL 38

    Accepted Solution

    The following should work.
    $DNofOU = "OU=MyCompany,DC=domain,DC=com"
    Get-ADComputer -filter * -searchbase $DNofOU -searchscope OneLevel -properties Description | ForEach `
        $newDesc = $_.Description -replace "[:/]","." -replace "[()]"," "
        Set-ADComputer $_.samaccountname -description $newDesc

    Open in new window

    LVL 28

    Expert Comment

    Good catch Foo - I really did not read this was for description.

    Irrylyn go ahead and ignore my post, I wrote something for a rename of the computer in AD and missed the word "description" too early in the morning without my coffee.

    Author Comment

    No worries, craig.  I caught that before I used it (I was lucky enough to have my coffee this morning!)  but it may very well come in handy at another time!

    foo:  this worked perfectly, thank you.

    You both have such beautiful minds!  Thank you again for your assistance.  EE blows me away every time!

    Author Closing Comment

    50 to craig for the work put into that script which I am sure I will need at some point.

    450 to Correct answer provided by foo

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now