Wednesday, August 13, 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 :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Input variables
$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.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Input variables
$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)
}


Reference:
http://www.sharepointdiary.com/2014/04/copy-users-from-one-sharepoint-group-to-another-using-powershell.html

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...