Add SPUser to SPGroup using Powershell

Adding user to SharePoint group manually is a painful process. Not to mention if you have tons of users using your SharePoint site.  To make life easier, I actually came out with a simple powershell script and XML (where you store the user mappings) to automate the process.

[sourcecode language=”powershell”]

$webUrl = Read-Host "Enter SharePoint Web URL"
if($webUrl -eq "")
{
$webUrl = "[Your default http]";
}
$web = Get-SPWeb $webUrl
$xml = [xml](Get-Content .\PumpUser.xml)
#Put your xml (in this case ‘PumpUser.xml’) in the same folder where you place your powershell script
foreach($group in $xml.Groups.Group)
{
$title = $group.Title
$spGroup = $web.SiteGroups[$title]
foreach($user in $group.Users.User)
{
$temp = $web.EnsureUser($user)
$spGroup.AddUser($temp)
Write-Host "Added "$temp" to "$spGroup.Name
}
}
[/sourcecode]

The xml goes like this.

[sourcecode language=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<Groups>
<Group>
<Title>Administrator Group</Title>
<Users>
<User>userLogin1</User>
<User>userLogin2</User>
</Users>
</Group>
</Groups>
[/sourcecode]

Simple and easy =)

Leave a Reply