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

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

Introduction

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

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *