Would some one help me with Powershell script Add Multiple Users in SharePoint Owners group
2 Answers
I think, best way is create a group and add users in it then make that group as owner of your target group.
To assign group as a onwer of another group.
$web = Get-SPWeb http://webUrl
$groupOwner = $web.SiteGroups["GroupA"] #new group,this group will own "GroupB",
$groupTarget = $web.SiteGroups["GroupB"]
$groupTarget.Owner = $groupOwner
$groupTarget.Update()
Change SharePoint group owner with Powershell to a SharePoint group
To create group and add user in it, this code will read from an XML file and create the group along with users.
XML file format
?xml version="1.0"?>
<Groups>
<Group name="Approvers" description="">
<Users>
<User>DOMAIN\pchilds</User>
<User>DOMAIN\tsmith</User>
</Users>
</Group>
</Groups>
Here is powershell to create the groups
#Get Site and Web objects
$site = Get-SPSite “http://portal”
$web = $site.RootWeb
#Get XML file containing groups and associated users
$groupsXML = [xml] (Get-Content ("C:\Path\Groups.XML"))
#Walk through each group node defined in the XML file
$groupsXML.Groups.Group | ForEach-Object {
#Check to see if SharePoint group already exists in the site collection
if ($web.SiteGroups[$_.name] -eq $null)
{
#If the SharePoint group doesn't exist already - create it from the name and description values at the node
$newGroup = $web.SiteGroups.Add($_.name, $web.CurrentUser, $null, $_.description)
}
#Get SharePoint group from the site collection
$group = $web.SiteGroups[$_.name]
#Add the users defined in the XML to the SharePoint group
$_.Users.User | ForEach-Object {
$group.Users.Add($_, "", "", "")
}
}
#Dispose of Web and Site objects
$web.Dispose()
$site.Dispose()
Ref: http://get-spscripts.com/2010/07/creating-multiple-sharepoint-groups-and.html
-
I have already exist Owners and Users group in SharePoint, Owner group has full access permissions. I run above code users are added in specific group But when I try to login my site with added users it throw an error "Sorry, this site hasn't been shared with you." Would you please assist me for the same.Ganesh Swami– Ganesh Swami2017-01-12 13:17:34 +00:00Commented Jan 12, 2017 at 13:17
Assuming you have user in txt file...below can be used...
$loc = Get-Location
$Users = Get-Content "$loc\Data\Users.txt"
$siteCollUrl = "http://sharepoint/sites/sitecollection/subsite"
$groupname ="Owner Group" // valid sharepoint group name
$web = Get-SPWeb -identity $siteCollUrl
$group = $SPWeb.SiteGroups[$groupname]
foreach ($User in $Users) {
$web.EnsureUser($User)
Set-SPUser -Identity $User -Web $siteCollUrl -Group $group
}