Thursday, April 17, 2014

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:

  • 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.
The above method could be relatively simpler when you have to copy or move few users between SharePoint groups. Wouldn't it be better to use the scripted way to copy multiple users in bulk?

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)
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

4 comments :

  1. Thank you - this was very helpful!

    ReplyDelete
  2. For SharePoint 2013 I had to change to following and then it worked. Thanks

    $SourceGroup = $web.SiteGroups[$SourceGroupName]
    $TargetGroup = $web.SiteGroups[$TargetGroupName]

    ReplyDelete
  3. 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" ?

    ReplyDelete
    Replies
    1. Hi Clint,
      If 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)
      }
      }

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...