PowerShell Tip: Adding Users Without an Email Address

June 2 2010 3 comments

Here’s another handy quickie: we have a bunch of users we want to add as owners when building a SharePoint site, but the adduser STSADM command won’t accept an empty email address. We don’t have real addresses for the users, nor do we want to make something up.

The SharePoint API doesn’t require that users have email addresses, though, so we can work around the problem.


function Adduser([string]$siteUrl, [string]$userlogin,
    [string]$username, [string]$groupName)
    $site = new-object 'Microsoft.SharePoint.SPSite' $siteUrl
    $site.RootWeb.SiteUsers.Add($userlogin, "", $username, "")
    $user = $site.RootWeb.AllUsers[$userlogin]
    $group = $site.RootWeb.SiteGroups[$groupName]
    trap [Exception] {

By now, most of this should look rather familiar. The only new thing is the “trap” construct, which acts much like the “try/catch” construct familiar from C# and a number of other languages.

If you’re wondering about the duplicated Dispose() call, it’s because PowerShell doesn’t come with a “using” construct, or even “try/finally”, so it seems we’re left with the unfortunate alternative of repetitive code. There is a better way, but it’s beyond the scope of this blog — check out Adam Weigert’s implementations of try/catch/finally and using. In addition to being really handy, they serve as a great example of how flexible PowerShell is.

Back to our issue at hand, let’s define a list of the users we want to grant owner access to:

$users = @(
    ,('DOMAIN\janedoe', 'Jane Doe')
    ,('DOMAIN\johndoe', 'John Doe')
) | foreach { New-Object PSObject -prop @{ Login = $_[0]; Name = $_[1] } }

I don’t much care for repeating the property names on each line, so instead we’ll build the list as a simple, untyped nested list, but then transform it into a list of objects with named properties.

Now, let’s enumerate all the site collections in a web application:

$sites = [xml](stsadm -o enumsites -url 'http://webapplication')

and then work through the site collections, first querying for the full name of the owner group and then adding each user to that group:

$sites.Sites.Site | foreach {
    $site = $_.Url;
    $groups = [xml](stsadm -o enumgroups -url $site)
    $ownerGroup = ($groups.Groups.Group | where {$_.Name -match "Owners" })
    $users | foreach {
        AddUser $site $_.Login $_.Name $ownerGroup.Name

This is fine for a small number of users, for a larger number, you’ll want to modify AddUser so that it can process a list of users, and won’t have to re-open the site for every user.

That’s it for today. Have fun!

Popularity: 1% [?]

3 comments to “PowerShell Tip: Adding Users Without an Email Address”

  1. anamika mukherjee says:

    add user method is giving errors it does accept 1 argument

  2. rambarat says:

    how to add user with email address

  3. This hit marker is especially good for long distance situations.

    This is probably the most important move you have to
    make. To put things in perspective, the highest
    grossing film at the time, Star Wars, grossed 486 million netting a profit of 175 million.

Leave a Reply