Home > Sharepoint > Adding User to Group in Sharepoint

Adding User to Group in Sharepoint

Adding Users to a specified group in Sharepoint can be done through the AddUser method on a specific group.

First we have to create a variable around the group that we want to work with, in this example we will use the group that was created in the Adding a Custom Group to Sharepoint post.


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

PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb(); $TheNewGroup = $OpenWeb.SiteGroups | Where-Object {$_.Name -match "The New Group"}; $OpenWeb.Dispose(); $SPSite.Dispose()

Now that we have an object containing our group we need all the users that we want to add to the group. You can filter out specific users with the Where-Object CmdLet, but in this example im just going for all users. I only need the LoginName, Email and Name of the User so I only select those properties with the Select-Object CmdLet


PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb(); $User = $OpenWeb.Users | Select Name, Email, LoginName; $OpenWeb.Dispose(); $SPSite.Dispose()

Last step is to add the Users in my object to The New Group.


PS > $User | ForEach-Object {
	$TheNewGroup.AddUser(
		$_.LoginName,
		$_.Email,
		$_.Name,
		""
	)
}

Rating 3.00 out of 5
[?]
Categories: Sharepoint Tags:
  1. No comments yet.
  1. No trackbacks yet.

Spam Protection by WP-SpamFree