FlCo
FlCo

Reputation: 71

Is there any way to add a user to multiple groups in sharepoint from CSV?

I was wondering how to add a single user to multiple groups through CSV? This is what I have tried, but won't work.

$SPWeb = Get-SPWeb “https://sharepoint"

$SPList = $SPWeb.Lists["LISTNAME"]

$exportlist = @()

$SPList.Items | foreach {

$obj = New-Object PSObject -Property @{

’SharePointAccessGroup’= $_["SharePointAccess"]

’samaccountnameSharePoint’= "i:0#.w|domain\" + $_["SAMAccountName"]

} $exportlist += $obj

$exportlist | Export-Csv -path ‘C:\SharePointGroup.csv’ -noType

}

$dataa = import-Csv -Path "C:\SharePointGroup.csv" -Delimiter ";"

foreach ($s in $dataa)

{

$SharePointAccessGroup = $s.SharePointAccessGroup

$samaccountnameSharePoint = $s.samaccountnameSharePoint

$Groups=($s.SharePointAccessGroup)

foreach ($g in $Groups) {

set-SPUser -Identity $s.samaccountnameSharePoint -Web https://sharepoint -Group $g

}

Upvotes: 0

Views: 170

Answers (1)

David Drever
David Drever

Reputation: 801

So if I am reading your code correctly, it looks like you are trying to add a user but using the internal designation (i:0#.w|...) This won't work with set-SPUser. Simply use Domain\username. Also, you may want to use New-SPUser if the user is not in the UIL.

New-SPUser -UserAlias <domain\username> -Web "https://sharepoint" -Group $g

Hope this helps.

Upvotes: 1

Related Questions