Copy Users from One SharePoint Group to Another using PowerShell
Recently, I had a requirement to copy users from one SharePoint group to another group. Unfortunately, SharePoint doesn't support nested groups. Simply renaming the group didn't help us! Well, There is no direct way to copy users from one group to another, but without having to retype each user IDs, you can use this trick to save time in copy-move SharePoint users:
PowerShell Script to Copy/Move Users from One SharePoint Group to Another :
Copy users from one SharePoint site collection to another using PowerShell:
The above script copies users between SharePoint groups of the same site. Can we copy users between groups in different site collections? Why not? Lets change the above script slightly to copy users from one SharePoint site group to another.
- Go to Site Actions >> Site Settings
- Click on "People and Groups" under Users and Permissions group
- Pick required users from the source group. Click on "E-mail Users" from "Actions" menu.
- This launches the default E-mail client such as Outlook. Now you can copy those IDs.
- Head-on to the Target SharePoint group >> Add users and paste those copied IDs.
PowerShell Script to Copy/Move Users from One SharePoint Group to Another :
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Input variables $WebURL = "http://sharepoint.crescent.com/sites/Operations/" $SourceGroupName="Operations Members" $TargetGroupName="Operations Owners" #Get the Web $web = Get-SPWeb $WebURL #Get the Source and Target Groups $SourceGroup = $web.groups | where {$_.name -eq $SourceGroupName } $TargetGroup = $web.groups | where {$_.name -eq $TargetGroupName } #Iterate through each users in the source group foreach ($user in $SourceGroup.users) { $TargetGroup.AddUser($user) Write-Host "Copied $user from $SourceGroup to $TargetGroup" #To move users, Just remove them from source group #$SourceGroup.RemoveUser($user) }
Copy users from one SharePoint site collection to another using PowerShell:
The above script copies users between SharePoint groups of the same site. Can we copy users between groups in different site collections? Why not? Lets change the above script slightly to copy users from one SharePoint site group to another.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Input variables $SourceWebURL = "http://sharepoint.crescent.com/sites/Operations/" $TargetWebURL = "http://sharepoint.crescent.com/sites/Marketing/" $SourceGroupName="Operations Members" $TargetGroupName="Marketing Members" #Get the Webs $SourceWeb = Get-SPWeb $SourceWebURL $TargetWebURL= Get-SPWeb $TargetWebURL #Get the Source and Target Groups $SourceGroup = $SourceWeb.groups | where {$_.name -eq $SourceGroupName } $TargetGroup = $TargetWebURL.groups | where {$_.name -eq $TargetGroupName } #Iterate through each users in the source group foreach ($user in $SourceGroup.users) { $TargetGroup.AddUser($user) Write-Host "Copied $user from $SourceGroup to $TargetGroup" #To move users, Just remove them from source group #$SourceGroup.RemoveUser($user) }
Thank you - this was very helpful!
ReplyDeleteFor SharePoint 2013 I had to change to following and then it worked. Thanks
ReplyDelete$SourceGroup = $web.SiteGroups[$SourceGroupName]
$TargetGroup = $web.SiteGroups[$TargetGroupName]
What if we need to go from multiple Sharepoint groups to a single Sharepoint group? Do we just do something like $SourceGroupName="Operations Members" AND "Finance Members" ?
ReplyDeleteHi Clint,
DeleteIf you want to move from Multiple groups, the code goes like this:
$SourceGroupNames="Operations Members", "Operations Owners", "Operations Visitors"
$TargetGroupName="Operations Users"
#Iterate through each source group
foreach($GroupName in $SourceGroupNames)
{
#Get the Source and Target Groups
$SourceGroup = $SourceWeb.groups | where {$_.name -eq $GroupName }
$TargetGroup = $TargetWebURL.groups | where {$_.name -eq $TargetGroupName }
#Iterate through each users in the source group
foreach ($user in $SourceGroup.users)
{
$TargetGroup.AddUser($user)
Write-Host "Copied $user from $SourceGroup to $TargetGroup"
#To move users, Just remove them from source group
#$SourceGroup.RemoveUser($user)
}
}
How can we do if Target is SharePoint online URL??
ReplyDeleteHello,
ReplyDeleteIf we want to move Sharepoint Groups users into sharepoint custom list which is having two columns 1) Email 2)access type.