Rename the bulk of sharepoint groups through powershell

Chandra Mounika Katta 26 Reputation points
2022-05-27T09:38:12.69+00:00

Hi,

We are planning to migrate the users and groups in active directory from one OU to another OU with Changing the group names.

i.e., Example: conso\testgroup this group changed to same domain conso\testgroup12

Above account having the access in SharePoint site at multiple locations.
Now AD changed the name to conso\testgroup12

Now it is fetch and reflected in SharePoint User Profiles but not in SharePoint sites.
There is 100 of Ad groups we have to change.

By manually we updated the group name in SharePoint by deleting old group and adding the new group.
But above method is not possible to upadte 100 groups manually.

Please suggest and update the powershell script for this activity.

We have tried the below articles and some powershell scripts, but there is no progress.

Articles::
Changes in Active Directory not reflected in SharePoint user info (microsoft.com)
AD Group Name Changes Not Updated After Profile Sync (microsoft.com)
AD Groups cannot be used to assign permissions in SharePoint (microsoft.com)
https://www.sharepointdiary.com/2015/05/update-user-display-name-in-sharepoint-2013-using-powershell.html
https://www.c-sharpcorner.com/Blogs/how-to-rename-a-sharepoint-group
https://godwinjogarajah.wordpress.com/2013/04/30/renaming-sharepoint-group-through-powershell/
https://support.microsoft.com/en-us/topic/99e44667-4c1a-496d-9dd3-7d7b0fee6179

Scripts::

STSADM –o migrategroup –oldlogin conso\testgroup –newlogin conso\testgroup12 -ignoresidhistory

[Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=Neutral, PublicKeyToken=71e9bce111e9429c")
$site = New-Object -TypeName Microsoft.SharePoint.SPSite -ArgumentList https://sharepoint
$group = $site.RootWeb.SiteUsers["conso\testgroup"]
$group.Name = "conso\testgroup12"

$group.Update()

Get-SPUser -Group "conso\testgroup" -Web "https://sharepoint" | Set-SPUser -Group "conso\testgroup12" -SyncFromAD -Confirm

Set-SPUser "conso\testgroup12" -SyncFromAD -web https://sharepoint

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

User Account to Sync and Site where the account exists

$UserAccount="conso\testgroup"
$WebURL="https://sharepoint"

Get the User's Current Display Name and E-mail

Get-SPUser -Identity $UserAccount -Web $WebURL

Force Sync from Active Directory

Set-SPUser -Identity "conso\testgroup12" -Web $WebURL -SyncFromAD

$user = Get-User 'conso\testgroup' | Rename-Object -NewName 'conso\testgroup12' -Passthru

$user | Set-User -DisplayName $newname

Get-SPSite -Limit All | Get-SPWeb | Foreach-object {
Set-SPUser -Identity "conso\testgroup" -DisplayName "conso\testgroup12" -Web $_ }

Microsoft 365 and Office | SharePoint Server | For business
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. WhTurner 1,611 Reputation points
    2022-05-27T14:27:44.007+00:00

    The tag "small-basic" is for Technical questions about Microsoft Small Basic, the only text-based language and IDE built for students to learn to code

    Please look for a more fitting tag as your question in not about Small Basic

    0 comments No comments

  2. Tong Zhang_MSFT 9,251 Reputation points
    2022-05-30T05:46:44.6+00:00

    Hi @Chandra Mounika Katta ,

    Based on my research and testing, you can use the following code to rename SharePoint groups using PowerShell:

    #Load SharePoint CSOM Assemblies  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
       
    #Function to Rename a Group  
    Function Rename-SPOGroup([String]$SiteURL, [String]$OldGroupName,[String]$NewGroupName)  
    {  
        Try {  
            #Get credentials to connect  
            $Cred= Get-Credential  
            $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
        
            #Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
            $Ctx.Credentials = $Credentials  
       
            #Get the Group by name  
            $Group=$Ctx.web.SiteGroups.GetByName($OldGroupName)  
       
            If($Group -ne $Null)  
            {  
                #Rename the group  
                $Group.Title = $NewGroupName  
                $Group.Update()  
                $Ctx.ExecuteQuery()  
       
                write-host -f Green "Group '$OldGroupName' Renamed to '$NewGroupName' Successfully!" $_.Exception.Message  
            }  
        }  
        catch {  
            write-host "Error Renaming Group: $($_.Exception.Message)" -foregroundcolor Red  
        }  
    }  
       
    #Variables  
    $SiteURL="https://Crescent.sharepoint.com/"  
    $OldGroupName = "Team Site Members"  
    $NewGroupName = "Marketing Managers"  
       
    #Call the function  
    Rename-SPOGroup -SiteURL $SiteURL -OldGroupName $OldGroupName -NewGroupName $NewGroupName  
    

    You can create a csv file that contains the SharePoint Groups Names that need to be renamed (OldName and NewName), then import the csv file through PowerShell and get the data in the file to rename the Groups in bulk.

    Here is the code I tested, please refer to:

    #Load SharePoint CSOM Assemblies  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
       
    #Function to Rename a Group  
    Function Rename-SPOGroup([String]$SiteURL, [String]$OldGroupName,[String]$NewGroupName)  
    {  
            #Get credentials to connect  
            $Cred= Get-Credential  
            $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
        
            #Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
            $Ctx.Credentials = $Credentials  
       
            #Get the Group by name  
            $Group=$Ctx.web.SiteGroups.GetByName($OldGroupName)  
       
            If($Group -ne $Null)  
            {  
                #Rename the group  
                $Group.Title = $NewGroupName  
                $Group.Update()  
                $Ctx.ExecuteQuery()  
       
                write-host -f Green "Group '$OldGroupName' Renamed to '$NewGroupName' Successfully!" $_.Exception.Message  
            }  
      
    }  
       
    #Variables  
    $SiteURL="https://xxxxx.sharepoint.com/sites/zellatest"  
    $groups=Import-Csv -Path C:\Users\Administrator\Desktop\test.csv   
      
    foreach ($group in $groups)  
    {  
    $OldGroupName= $group.OldName  
    $NewGroupName=$group.NewName  
    Rename-SPOGroup -SiteURL $SiteURL -OldGroupName $OldGroupName -NewGroupName $NewGroupName  
    }  
    

    CSV file:
    206601-image.png
    My test result:
    206611-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  3. Chandra Mounika Katta 26 Reputation points
    2022-06-28T11:52:16.227+00:00

    When a user is updated in AD , the User profile sync will update it in the UPSA

    From there two timers jobs update them on Site collection

    But for Group update in AD are not synched to user profiles or site collections - this feature is NOT part of any timer jobs

    hence we need to update it manually using PowerShell

    Below script will update the display name of renamed AD group name in SharePoint by comparing with AD group name in active directory

    ---------------------------------------------------------------------------------------------------------------------------

    param (
    [Parameter (mandatory=$True)][string]$SiteCollection = $(throw "Missing SiteCollection (Eg: -SiteCollection http://sp/)"),
    [Parameter (mandatory=$false)][switch]$ConfirmSync
    )

    clear
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    if ($snapin -eq $null)
    {
    Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }

    If($ConfirmSync -eq $true)
    {
    Write-Host "Warning ! The script is being executed in the read-write mode. Please review the report after the script execution" -ForegroundColor Magenta
    Write-Host " "
    sleep 2
    }
    else
    {
    Write-Host "Info : The script is being executed in the ReadOnly mode. Please review the report after the script execution" -ForegroundColor Green
    Write-Host " "
    sleep 2
    }

    try
    {
    $site=get-spsite $SiteCollection -ErrorAction stop
    }
    catch
    {
    write-host "Cannot find an SPSite object with Id or Url: " $SiteCollection -foregroundcolor Yellow
    break;
    }

    Declaring and creating the log files. Each time the script is executed, a new file will be created with the current time in the filename.

    $dateTime=Get-Date -format "dd-MMM-yyyy HH-mm-ss"
    $ReportFile="Sync_SPADSecurityGroupInfo_Log"+"_"+ $dateTime + ".csv"
    $ExceptionFile="Sync_SPADSecurityGroupInfo"+"_Exception"+ $dateTime + ".log"
    $Header="SiteCollectionURL;UserLogin;SID_in_SP;Name_in_SP;Name_in_AD;Match;Sync"
    Add-Content -Path $ReportFile -Value $Header

    $AllDomainGroups = Get-SPUser -Web $SiteCollection -Limit ALL| where {$_.IsdomainGroup -eq $True}
    write-host "=================================================="
    write-host "Site collection:" $SiteCollection -NoNewline -ForegroundColor Magenta
    Write-Host " || " -NoNewline
    write-host "No of AD Groups:" $AllDomainGroups.count -ForegroundColor Magenta
    write-host "=================================================="

    Write-Host ""

    foreach ($domaingroup in $AllDomainGroups)
    {
    [String]$UserLogin=$domaingroup.UserLogin

    if ($UserLogin.StartsWith("c:0+.w|s"))  
    {  
    
        $Name_in_SP = $domaingroup.DisplayName  
        $SID_in_SP = ($UserLogin.Split("|")[1]).ToString()  
    
        try  
        {  
        $objSID = New-Object System.Security.Principal.SecurityIdentifier ($SID_in_SP)  
        $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])  
        $Name_in_AD = $objUser.Value  
        }  
        catch [Exception]  
        {  
        write-host "Exception occured while fetching the details of" $UserLogin " from Active directory. Please review the exception log file"  
        write-host $_.exception  
        $GroupresolutionException="Exception occured while fetching the details of" +$UserLogin+"from Active directory"  
        Add-Content -Value $GroupresolutionException -Path $ExceptionFile  
        Add-Content -Value $_.exception -Path $ExceptionFile  
        Add-Content -Value "=====================================================" -Path $ExceptionFile  
        Add-Content -Value "" -Path $ExceptionFile  
        }  
    
            if ($Name_in_SP -eq $Name_in_AD)  
            {  
            $match="Yes"  
            }  
            else  
            {  
            $match="No"  
            }  
    
        Write-host "Fetching group"$domaingroup -ForegroundColor Yellow  
        write-host "UserLogin :" $UserLogin  
        write-host "SID_in_SP :" $SID_in_SP  
    
        Write-host "Name_in_SP:" $Name_in_SP  
        write-host "Name_in_AD:" $Name_in_AD  
        write-host "Match     :" $match  
        write-host ""  
      
            if ($match -eq "No" -and $ConfirmSync -eq $true)  
            {  
                    write-host "Synching.." -ForegroundColor Green  
                    try  
                    {  
                    $domaingroup.DisplayName = $Name_in_AD  
                    $domaingroup.Update()  
                    }  
                    catch [Exception]  
                    {  
                    write-host "Exception occured while updating the details of" $UserLogin " on Site collection $SiteCollection. Please review the exception log file"  
                    write-host $_.exception  
                    $GroupUpdateException="Exception occured while updating the details of" + $UserLogin + " on Site collection $SiteCollection"  
                    Add-Content -Value $GroupUpdateException -Path $ExceptionFile  
                    Add-Content -Value $_.exception -Path $ExceptionFile  
                    Add-Content -Value "=====================================================" -Path $ExceptionFile  
                    Add-Content -Value "" -Path $ExceptionFile  
                    }  
                    $Header="SiteCollectionURL;UserLogin;SID_in_SP;Name_in_SP;Name_in_AD;Match;Sync"  
                    $MessageSync=$SiteCollection+";"+$UserLogin+";"+$SID_in_SP+";"+$Name_in_SP+";"+$Name_in_AD+";"+$match+";Yes"  
                    Add-Content -Path $ReportFile -Value $MessageSync  
            }  
            else  
            {  
                    $MessageNoSync=$SiteCollection+";"+$UserLogin+";"+$SID_in_SP+";"+$Name_in_SP+";"+$Name_in_AD+";"+$match+";No"  
                    Add-Content -Path $ReportFile -Value $MessageNoSync  
            }  
    }  
    else  
    {  
    Write-host "Fetching group"$domaingroup ".." -ForegroundColor Yellow -NoNewline  
    write-host ".. Skipping !" -ForegroundColor Cyan  
    write-host ""  
    $MessageSkip=$SiteCollection+";"+$UserLogin+";"+$SID_in_SP+";N/A;N/A;N/A;N/A"  
    Add-Content -Path $ReportFile -Value $MessageSkip  
    }  
    

    }

    -------------------------------------------------------------------------------------------------

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.