#Group,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10 #Initiators,John Doe,Nicholas Smith,Jonathan Smith,,,,,,, #Reviewers,Nicholas Smith,Jonathan Smith,Dave Smith,Tosha Doe,,,,,, # Read CSV and populate groups $webUrl = "http://acme.com/sites/acme/PremiumPayment2" $csvPath = "C:\Temp\20150427\UserGroupSetup.csv" $csv = Import-Csv -Path $csvPath $w = get-spweb $webUrl function EmptyGroup($g){ "Cleaning group: $($g.Name)" while($g.Users.Count -gt 0){ $u = $g.Users[0] "Removing user: $($u.DisplayName)" $g.RemoveUser($u) } } foreach($gg in $csv){ $gName = $gg.Group "Checikng if exists: `"$($gName)`"" $g = $w.SiteGroups[$gName] if($g -eq $null){ throw "Group `"$($gName)`" not found" } } $uniqueNames = new-object -TypeName "System.Collections.Generic.List[string]" foreach($gg in $csv){ $gName = $gg.Group for($i = 1; $i -le 10; $i++){ $name = $gg."P$($i)" if($name -ne $null -and $name -ne ""){ if(-not $uniqueNames.Contains($name)){ $uniqueNames.Add($name) } } } } $allUsers = get-spuser -Web $w.Url $userDic = new-object -TypeName "System.Collections.Generic.Dictionary[string,Microsoft.SharePoint.SPUser]" "Checikng users" foreach($name in $uniqueNames){ $user = $allUsers |?{$_.DisplayName -eq $name } if($user -eq $null){ throw "User not found: $($name)" } else { if($($user.GetType().Name) -eq "SPUser"){ $userDic.Add($name, $user) } else { $acmeUser = $user|?{$_.UserLogin.StartsWith("ACME\")} $userDic.Add($name, $acmeUser) } } } foreach($gg in $csv){ $gName = $gg.Group "Processing group: `"$($gName)`"" $g = $w.SiteGroups[$gName] EmptyGroup $g for($i = 1; $i -le 10; $i++){ $name = $gg."P$($i)" if($name -ne $null -and $name -ne ""){ $user = $userDic[$name] $g.AddUser($user) } } }