Tuesday, July 9, 2013

My First PowerShell Script

What It Does

Finds a list of Active Directory groups, based on a filter. For each one found, rename it to something else. Write back the change to Active Directory.

The Code

$theList = Get-ADGroup -Filter {sAMAccountName -like "SomeGroupPrefix*"} | Select Name, sAMAccountName, DistinguishedName

foreach ($i in $theList) {
$newName = $i.name -replace "Some", "Any"
#write-host $i.name, $newName
Rename-ADObject -Identity $i.DistinguishedName -NewName $newName
}