How to mass change the primary group of users in active directory with PowerShell

Intro

This very simple PS code will allow you to change the primary group of a group of users and report back who the users are you've changed through the script.

Code

Start-Transcript "C:\Users\UserName\Downloads\PrimaryGroupChange.log"

$newPrimaryGroup = get-adgroup "NewPrimaryGroupHere" -Properties "primaryGroupToken"
$oldPrimaryGroup = get-adgroup "OldPrimaryGroupHere" -Properties "primaryGroupToken"

$tempUsers = get-aduser -Filter {PrimaryGroupID -eq $oldPrimaryGroup.primaryGroupToken} -SearchBase "OU=Your,OU=Custom,OU=OUHere,DC=contoso,DC=com"

Write-Host "Total users to be changed = $($tempUsers.Count)"

ForEach($user in $tempUsers)
{

$tempUser = get-aduser -Identity $user | Select-object name Write-Host "Changing primary group of $($tempUser)" get-aduser -Identity $user | set-aduser -replace @{primaryGroupID=$newPrimaryGroup.primaryGroupToken}

}

Stop-Transcript

Comments

Loading Comments...