• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

Replace character in AD description for all computer accounts

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 '.'
and
If '/' exists, replace it with '.'
and
If '(' exists, replace it with ' ' (space)
and
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!
0
Irrylyn
Asked:
Irrylyn
  • 2
  • 2
2 Solutions
 
becraigCommented:
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

0
 
footechCommented:
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

0
 
becraigCommented:
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.
0
 
IrrylynAuthor Commented:
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!
0
 
IrrylynAuthor Commented:
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now