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)
Write-Host "Added "$temp" to "$spGroup.Name

The xml goes like this.

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

Simple and easy =)

Leave a Reply

Your email address will not be published. Required fields are marked *