.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> SharePoint >> Post New QuestionBookmark and Share Subscribe to Forum

Add Multiple users to sharepoint Members group using powershell

Posted By: Shruthi BM     Posted Date: March 28, 2011    Points:5   Category :SharePoint
Hi All,

Can you please help me to add multiple users to sharepoint members groups of one site using powershell.

I have email ids of all users.
This is very urgent.

Regards,
Shruthi BM

Responses
Author: Mahesh          Accepted Answer   
Posted Date: March 28, 2011     Points: 40   

Hi Shruthi,
try to use following script, this script add users who are having read permissions,

function Add-SPUser([string]$url, [string]$Role, [string]$Domain, [string]$sAMAccountName, [string]$Mail, [string]$DisplayName) {

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$SPSite = New-Object Microsoft.SharePoint.SPSite($url);
$OpenWeb = $SpSite.OpenWeb(); $OpenWeb.Roles | Select Name, Description; $OpenWeb.Dispose(); $SPSite.Dispose()

$OpenWeb.Roles[$Role].AddUser(
$Domain + $sAMAccountName,
$Mail,
$DisplayName,
""
)
$OpenWeb.Dispose()
$SPSite.Dispose()
}

PS > $User | foreach-object {
Add-SPUser -url http://wss -Role Read -Domain bpa -sAMAccountName $_.sAMAccountName -Mail $_.Mail -DisplayName $_.DisplayName
}


for more information please follow the link,
http://www.powershell.nu/2009/01/06/adding-users-to-sharepoint/

Thanks & Regards
Mahesh

Author: Shruthi BM             
Posted Date: March 29, 2011     Points: 20   

Hi Mahesh,

Thanks a lot!!!!

But i have one query, from where its picking account name,email,display name.

I have all details in excel file. Please help me.

Regards,
Shruthi BM

Author: Mahesh          Accepted Answer   
Posted Date: March 29, 2011     Points: 40   

Hi Shruthi,
in above code name, email etc. were taken manually,
but you can read excel file and take values in variables and then assign them in above code as,

$path = "$pwd\book.xls"

$excel = New-Object "Microsoft.Office.Interop.Excel.ApplicationClass"
$excel.Visible = $false
$excel.ScreenUpdating = $false
$excel.DisplayAlerts = $false

$workbooks = $excel.workbooks
$workbook = $workbooks.open($path)
$sheets = $workbook.worksheets
$activesheet = $sheets.item("Sheet1")
$range = $activesheet.range("A1","A1")
$range.Value2

ForEach ($item In $workbooks) { $item.Saved = $true ; $item.Close() }
$excel.Quit()
$range = $sheets = $activesheet = $workbook = $excel = $null

just add the code for error handling in it....

Thanks & Regards
Mahesh

Author: Ravishankar       [Moderator]      
Posted Date: May 21, 2011     Points: 20   

Very helpful Mahesh,You can put the code in code snipset section so that next time other people can use the same. Its saves the time lot. :)

Ravishankar Singh


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend