3

I am looking for script to add multiple users and multiple groups to SharePoint 2013. I have CSV file with cointains two columns with header "user" login and "group" name with are separated by (;) as a delimiter. There is more than 600 users which are references to 82 different groups. Script should also set permission "read" to all groups.

Thanks lot Michal

3

Just a five minute pseudocode, this should work...

add-pssnapin microsoft.sharepoint.powershell

$SPWeb = get-spweb http://sharepoint/sites/sitecollection/subsite
$owner = "domain\adminuser"
$op = New-SPClaimsPrincipal $OwnerName -IdentityType WindowsSamAccountName
$owner = $SPWeb | Get-SPUser $op


$csvlocation = "c:\csv\users.csv"
$values = Import-Csv $($csvlocation)| Sort-Object group

$count = $values.Count
$groups = @()
for($i=0; $i -lt $count; $i++){

  $item = $values[$i]
  $groupname = $item.group
  if(!$group.Contains($groupname))
  {
    $SPWeb.SiteGroups.Add($groupname, $owner, $null, $null) 
    $SPGroup = $SPWeb.SiteGroups[$groupname]
    $groups += $groupname
  }

    $mem = New-SPClaimsPrincipal $item.user -IdentityType WindowsSamAccountName
    $member = $SPWeb | Get-SPUser $mem
    $SPGroup = $SPWeb.SiteGroups[$groupname]
    $SPGroup.AddUser($member)


}
  • Thanks Mike we are almost there however I get an error : New-SPClaimsPrincipal : Cannot validate argument on parameter 'Identity'. The a rgument is null or empty. Supply an argument that is not null or empty and then try the command again. At C:\Users\SpSetupUsr\desktop\Untitled1.ps1:5 char:29 + $op = New-SPClaimsPrincipal $OwnerName -IdentityType WindowsSamAccountName ++ CategoryInfo : InvalidData: (:) [New-SPClaimsPrincipal], Parame terBindingValidationException+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SharePoint.PowerShell.SPCmdletNewClaimsPrincipal – Michał Dem Dec 29 '15 at 20:35
  • Get-SPUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again. At C:\Users\SpSetupUsr\desktop\Untitled1.ps1:6 char:30 + $owner = $SPWeb | Get-SPUser $op + ~~~ + CategoryInfo : InvalidData: (:) [Get-SPUser], ParameterBindingV alidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Share Point.PowerShell.SPCmdletGetUser – Michał Dem Dec 29 '15 at 20:35
  • my csv file looks like if that might help also : column 1 | column 2 user group domain\user HR domain\user2 HR domain\user3 LADP ... – Michał Dem Dec 29 '15 at 20:35
  • is the header of your CSV column1 and column2 instead of group and users? – Mike Dec 29 '15 at 20:38
  • nope is "user" and "group" header – Michał Dem Dec 29 '15 at 20:42
1

powershell that iterate thru *.CSV file (with login and group names) and add multiple users, groups and group permissions as necessary.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Write-Host "Connect Sharepoint Cmd-Let" 
Add-PSSnapin Microsoft.SharePoint.PowerShell 
}

$Site = Get-SPSite https://mysharepoint
$Web = $Site.RootWeb

$ownerName = "DOMAIN\SpAdmin"
$op = New-SPClaimsPrincipal $OwnerName -IdentityType WindowsSamAccountName
$owner = $Web | Get-SPUser $op

$csvlocation = "c:\csv\users.csv"
$values = Import-Csv $($csvlocation) -Delimiter ";" 

$values | ForEach-Object {

#Checking this group already exist in SP   
if(!$Web.SiteGroups[$_.group] -eq $_.group) {

    #Adding Group and Set Permission
    $Web.SiteGroups.Add($_.group, $owner, $null, $null) 
    $SPGroup = $Web.SiteGroups[$_.group]
    $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($Web.SiteGroups[$_.group])
    $role = $web.RoleDefinitions["Read"]
    $assignment.RoleDefinitionBindings.Add($role);
    $web.RoleAssignments.Add($assignment)
}

#Adding User to Group
$SPGroup = $Web.SiteGroups[$_.group]
$SPGroup.AddUser($_.user,"","",$_.group) 

#Added User and Group listed on console 
Write-Host "User :" $_.user "added to Group " $_.group

}

$web.Dispose()
$site.Dispose()
0

I need to import a .csv file with following fields:

GroupName,Managed_by,PrimarySmtpAddress,Members

Managed_by and Members are columns (People and Group) and both have multiple users separated ";"

Kindly advise

  • Perhaps, this doesnot answer the question, you can raise a new question or post comments to the main question. Thanks – Gaurravs May 31 '16 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.