2015-11-16

Rename Active Directory groups with PowerShell

I was struggling with this one because there are several different name attributes for AD objects, name, DisplayName, SamAccountName, cn, etc.

I finally ended up with this solution which seems to rename all name attributes:


Import-Module ActiveDirectory

$TargetGroups = Get-ADGroup -Filter {Name -like "XXXDescription*"}

ForEach($SingleGroup in $TargetGroups)
{
    Write-Host "Old name: $($SingleGroup.Name)"
    $NewGroupName = $($SingleGroup.Name).Replace('XXXDescription','YYYYZZZZDescription')
    Write-Host "New name: $($NewGroupName)"
    Get-ADGroup $SingleGroup | Set-ADGroup -DisplayName $NewGroupName -SamAccountName $NewGroupName
    Get-ADGroup $SingleGroup | Rename-ADObject -NewName $NewGroupName
}
If there is a nicer solution, please feel free to post it in the comments.

No comments:

Post a Comment